def test_incorrect_format(self): Inventory( os.path.join(self.test_dir, 'inventory', 'test_incorrect_format'))
def run(self): Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) variable_manager = VariableManager() loader = DataLoader() options = Options( listtags=False, listtasks=False, listhosts=False, syntax=False, connection='smart', module_path='/usr/lib/python2.6/site-packages/ansible/modules/', forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user='******', verbosity=None, check=False) passwords = dict(vault_pass='******') inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='config/hosts') variable_manager.set_inventory(inventory) play_source = dict( name="Ansible Play", hosts='all', gather_facts='no', tasks=[ dict(action=dict(module=self.module, args=self.command), register='shell_out'), #dict(action=dict(module='shell', args='id'), register='shell_out'), #dict(action=dict(module='shell', args=dict(msg='{{shell_out.stdout}}'))) ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) self.results_callback = ResultCallback() tqm._stdout_callback = self.results_callback result = tqm.run(play) finally: if tqm is not None: tqm.cleanup()
def get_hosts(hostfile, pattern): hosts = [] inventory = Inventory(hostfile) hosts = inventory.list_hosts(pattern) return hosts
def test_variable_manager_precedence(self): ''' Tests complex variations and combinations of get_vars() with different objects to modify the context under which variables are merged. ''' v = VariableManager() v._fact_cache = defaultdict(dict) fake_loader = DictDataLoader({ # inventory1 '/etc/ansible/inventory1': """ [group2:children] group1 [group1] host1 host_var=host_var_from_inventory_host1 [group1:vars] group_var = group_var_from_inventory_group1 [group2:vars] group_var = group_var_from_inventory_group2 """, # role defaults_only1 '/etc/ansible/roles/defaults_only1/defaults/main.yml': """ default_var: "default_var_from_defaults_only1" host_var: "host_var_from_defaults_only1" group_var: "group_var_from_defaults_only1" group_var_all: "group_var_all_from_defaults_only1" extra_var: "extra_var_from_defaults_only1" """, '/etc/ansible/roles/defaults_only1/tasks/main.yml': """ - debug: msg="here i am" """, # role defaults_only2 '/etc/ansible/roles/defaults_only2/defaults/main.yml': """ default_var: "default_var_from_defaults_only2" host_var: "host_var_from_defaults_only2" group_var: "group_var_from_defaults_only2" group_var_all: "group_var_all_from_defaults_only2" extra_var: "extra_var_from_defaults_only2" """, }) inv1 = Inventory(loader=fake_loader, variable_manager=v, host_list='/etc/ansible/inventory1') inv1.set_playbook_basedir('./') play1 = Play.load(dict( hosts=['all'], roles=['defaults_only1', 'defaults_only2'], ), loader=fake_loader, variable_manager=v) # first we assert that the defaults as viewed as a whole are the merged results # of the defaults from each role, with the last role defined "winning" when # there is a variable naming conflict res = v.get_vars(loader=fake_loader, play=play1) self.assertEqual(res['default_var'], 'default_var_from_defaults_only2') # next, we assert that when vars are viewed from the context of a task within a # role, that task will see its own role defaults before any other role's blocks = play1.compile() task = blocks[1].block[0] res = v.get_vars(loader=fake_loader, play=play1, task=task) self.assertEqual(res['default_var'], 'default_var_from_defaults_only1') # next we assert the precendence of inventory variables v.set_inventory(inv1) h1 = inv1.get_host('host1') res = v.get_vars(loader=fake_loader, play=play1, host=h1) self.assertEqual(res['group_var'], 'group_var_from_inventory_group1') self.assertEqual(res['host_var'], 'host_var_from_inventory_host1') # next we test with group_vars/ files loaded fake_loader.push( "/etc/ansible/group_vars/all", """ group_var_all: group_var_all_from_group_vars_all """) fake_loader.push( "/etc/ansible/group_vars/group1", """ group_var: group_var_from_group_vars_group1 """) fake_loader.push( "/etc/ansible/group_vars/group3", """ # this is a dummy, which should not be used anywhere group_var: group_var_from_group_vars_group3 """) fake_loader.push( "/etc/ansible/host_vars/host1", """ host_var: host_var_from_host_vars_host1 """) v.add_group_vars_file("/etc/ansible/group_vars/all", loader=fake_loader) v.add_group_vars_file("/etc/ansible/group_vars/group1", loader=fake_loader) v.add_group_vars_file("/etc/ansible/group_vars/group2", loader=fake_loader) v.add_host_vars_file("/etc/ansible/host_vars/host1", loader=fake_loader) res = v.get_vars(loader=fake_loader, play=play1, host=h1) self.assertEqual(res['group_var'], 'group_var_from_group_vars_group1') self.assertEqual(res['group_var_all'], 'group_var_all_from_group_vars_all') self.assertEqual(res['host_var'], 'host_var_from_host_vars_host1') # add in the fact cache v._fact_cache['host1'] = dict( fact_cache_var="fact_cache_var_from_fact_cache") res = v.get_vars(loader=fake_loader, play=play1, host=h1) self.assertEqual(res['fact_cache_var'], 'fact_cache_var_from_fact_cache')
def exec_ansible(module, args, host): import json from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.playbook.play import Play from ansible.executor.task_queue_manager import TaskQueueManager from ansible.plugins.callback import CallbackBase class ResultCallback(CallbackBase): def v2_runner_on_ok(self, result, **kwargs): host = result._host self.data = json.dumps({host.name: result._result}, indent=4) print(self.data) Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check']) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() # module_path参数指定本地ansible模块包的路径 options = Options(connection='smart', module_path='/usr/local/python3/lib/python3.6/site-packages/ansible/modules/', forks=100, become=None, become_method=None, become_user='******', check=False) passwords = dict(vault_pass='******') # Instantiate our ResultCallback for handling results as they come in results_callback = ResultCallback() # create inventory and pass to var manager # host_list指定本地ansible的hosts文件 inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Ansible Play", # hosts可以指定inventory中的一组主机,也可指定单台主机 hosts=host, gather_facts='no', # task执行列表,如果想一次执行多个任务,可以在列表中添加任务 tasks=[ dict(action=dict(module=module, args=args), register='shell_out'), ] ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=results_callback, ) result = tqm.run(play) finally: if tqm is not None: tqm.cleanup return json.loads(results_callback.data)
def run(self): super(PlaybookCLI, self).run() # Note: slightly wrong, this is written so that implicit localhost # Manage passwords sshpass = None becomepass = None vault_pass = None passwords = {} # don't deal with privilege escalation or passwords when we don't need to if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax: self.normalize_become_options() (sshpass, becomepass) = self.ask_passwords() passwords = { 'conn_pass': sshpass, 'become_pass': becomepass } loader = DataLoader() if self.options.vault_password_file: # read vault_pass from a file vault_pass = CLI.read_vault_password_file(self.options.vault_password_file, loader=loader) loader.set_vault_password(vault_pass) elif self.options.ask_vault_pass: vault_pass = self.ask_vault_passwords()[0] loader.set_vault_password(vault_pass) # initial error check, to make sure all specified playbooks are accessible # before we start running anything through the playbook executor for playbook in self.args: if not os.path.exists(playbook): raise AnsibleError("the playbook: %s could not be found" % playbook) if not (os.path.isfile(playbook) or stat.S_ISFIFO(os.stat(playbook).st_mode)): raise AnsibleError("the playbook: %s does not appear to be a file" % playbook) # create the variable manager, which will be shared throughout # the code, ensuring a consistent view of global variables variable_manager = VariableManager() variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.options) variable_manager.options_vars = load_options_vars(self.options) # create the inventory, and filter it based on the subset specified (if any) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory) variable_manager.set_inventory(inventory) # (which is not returned in list_hosts()) is taken into account for # warning if inventory is empty. But it can't be taken into account for # checking if limit doesn't match any hosts. Instead we don't worry about # limit if only implicit localhost was in inventory to start with. # # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts()) no_hosts = False if len(inventory.list_hosts()) == 0: # Empty inventory # display.warning("provided hosts list is empty, only localhost is available") no_hosts = True inventory.subset(self.options.subset) if len(inventory.list_hosts()) == 0 and no_hosts is False: # Invalid limit raise AnsibleError("Specified --limit does not match any hosts") # flush fact cache if requested if self.options.flush_cache: for host in inventory.list_hosts(): variable_manager.clear_facts(host) # create the playbook executor, which manages running the plays via a task queue manager pbex = PlaybookExecutor(playbooks=self.args, inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords) results = pbex.run() result_playbooks_object = [] result_json_object = {'playbooks': result_playbooks_object} if isinstance(results, list): for p in results: result_plays = [] result_playbook_object = { 'name' : p['playbook'], 'plays' : result_plays } result_playbooks_object.append(result_playbook_object) if not self.options.listtasksjson: display.display('\nplaybook: %s' % p['playbook']) for idx, play in enumerate(p['plays']): msg = "\n play #%d (%s): %s" % (idx + 1, ','.join(play.hosts), play.name) mytags = set(play.tags) msg += '\tTAGS: [%s]' % (','.join(mytags)) result_tasks = [] result_play_object = { 'name': play.name, 'pattern': play.hosts, 'tags': [x.encode('UTF8') for x in mytags], 'tasks': result_tasks } result_plays.append(result_play_object) if self.options.listhosts: playhosts = set(inventory.get_hosts(play.hosts)) msg += "\n pattern: %s\n hosts (%d):" % (play.hosts, len(playhosts)) for host in playhosts: msg += "\n %s" % host result_play_object['hosts'] = [str(x) for x in playhosts] if not self.options.listtasksjson: display.display(msg) all_tags = set() if self.options.listtags or self.options.listtasks: taskmsg = '' if self.options.listtasks: taskmsg = ' tasks:\n' def _process_block(b): taskmsg = '' for task in b.block: if isinstance(task, Block): taskmsg += _process_block(task) else: if task.action == 'meta': continue all_tags.update(task.tags) if self.options.listtasks: cur_tags = list(mytags.union(set(task.tags))) cur_tags.sort() result_task_object = {'tags':[x.encode('UTF8') for x in cur_tags]} result_tasks.append(result_task_object) if task.name: taskmsg += " %s" % task.get_name() result_task_object['name'] = task.get_name().encode('UTF8') else: taskmsg += " %s" % task.action taskmsg += "\tTAGS: [%s]\n" % ', '.join(cur_tags) return taskmsg all_vars = variable_manager.get_vars(loader=loader, play=play) play_context = PlayContext(play=play, options=self.options) for block in play.compile(): block = block.filter_tagged_tasks(play_context, all_vars) if not block.has_tasks(): continue taskmsg += _process_block(block) if self.options.listtags: cur_tags = list(mytags.union(all_tags)) cur_tags.sort() taskmsg += " TASK TAGS: [%s]\n" % ', '.join(cur_tags) if not self.options.listtasksjson: display.display(taskmsg) if self.options.listtasksjson: print(json.dumps(result_json_object, indent=4, sort_keys=True)) return 0 else: return results
def initialize_inventory(self): self.options['inventory_manager'] = Inventory( self.options['inventory'])
def complex_inventory(self): return Inventory(self.complex_inventory_file)
def dir_inventory(self): return Inventory(self.inventory_dir)
def large_range_inventory(self): return Inventory(self.large_range_inventory_file)
def script_inventory(self): return Inventory(self.inventory_script)
def simple_inventory(self): return Inventory(self.inventory_file)
def test_combined_range(self): i = Inventory( os.path.join(self.test_dir, 'inventory', 'test_combined_range')) hosts = i.list_hosts('test') expected_hosts = ['host1A', 'host2A', 'host1B', 'host2B'] assert sorted(hosts) == sorted(expected_hosts)
def test_alpha_end_before_beg(self): Inventory( os.path.join(self.test_dir, 'inventory', 'test_alpha_end_before_beg'))
def main(): host_list = ['localhost', 'www.example.com', 'www.google.com'] Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) # initialize needed objects variable_manager = VariableManager() loader = DataLoader() options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, verbosity=None, check=False) passwords = dict() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Ansible Play", hosts=host_list, gather_facts='no', tasks=[ dict( action=dict(module='command', args=dict( cmd='/usr/bin/uptime'))) ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None callback = ResultsCollector() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm._stdout_callback = callback result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() print "UP ***********" for host, result in callback.host_ok.items(): print '{} >>> {}'.format(host, result._result['stdout']) print "FAILED *******" for host, result in callback.host_failed.items(): print '{} >>> {}'.format(host, result._result['msg']) print "DOWN *********" for host, result in callback.host_unreachable.items(): print '{} >>> {}'.format(host, result._result['msg'])
def initialize_inventory(self): return Inventory( loader=self.loader, variable_manager=self.variable_manager, host_list=self.inventory_file )
parser = argparse.ArgumentParser(description='Process ansible inventory options') parser.add_argument("-l", "--list", action='store_true', help="list of groups" ) parser.add_argument("-H", "--host", help="dictionary of variables for host") args = parser.parse_args() def prettyprint(string): print json.dumps(string, indent=4, sort_keys=True) variable_manager = VariableManager() loader = DataLoader() if C.DEFAULT_VAULT_PASSWORD_FILE: loader.read_vault_password_file(C.DEFAULT_VAULT_PASSWORD_FILE) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=invetoryfile) variable_manager.set_inventory(inventory) #===== def getHosts(): hosts = {} for host in inventory.list_hosts(): vars = host.get_vars() del vars['inventory_hostname'], vars['inventory_hostname_short'] hosts[host.name] = vars return hosts def getGroups(): groups = {} group_objects = inventory.get_groups() for group_name in group_objects.keys():
def default(self): verbose = self.app.pargs.verbose init(autoreset=True) if len(self.app.pargs.extra_arguments) == 0: self.app.args.print_help() print(Fore.RED + 'Error! You must specify a playbook file to run') sys.exit(1) if str(self.app.pargs.become_method).upper() not in [ 'SUDO', 'SU', 'PBRUN', 'PFEXEC', 'DOAS', 'DZDO', 'KSU', 'RUNAS' ]: print( Fore.RED + 'Error! Become method must be one of sudo, su, pbrun, pfexec, doas, dzdo, ksu or runas.' ) sys.exit(1) #if len(self.app.pargs.extra_arguments) > 0: playbook_path = self.app.pargs.extra_arguments[0] if len( self.app.pargs.extra_arguments) > 0 else "site.yml" if not os.path.isfile(playbook_path): print(Fore.RED + 'Error! The playbook file does not exist') sys.exit(1) inventory_path = self.app.pargs.inventory if not os.path.isfile(inventory_path): print(Fore.RED + 'Error! The inventory file does not exist.') sys.exit(1) # Most of the code from here down is straight copy & paste from ansible source code, # with a few tweaks/hacks to clean up variables that we don't want to emit in the generated # YAML. loader = DataLoader() pb_cli = PlaybookCLI(sys.argv[1:]) pb_cli.parse() # !!!!! WARNING: THESE WILL BE INCLUDED IN THE GENERATED YAML FILE !!!!! (ssh_pass, become_pass) = pb_cli.ask_passwords() passwords = {'conn_pass': ssh_pass, 'become_pass': become_pass} vault_pass = None if self.app.pargs.ask_vault_pass: vault_pass = pb_cli.ask_vault_passwords() else: vault_pass = pb_cli.read_vault_password_file( self.app.pargs.vault_password_file, loader) if vault_pass is not None: loader.set_vault_password(vault_pass) # create the inventory, and filter it based on the subset specified (if any) host_list = self.app.pargs.inventory if self.app.pargs.inventory else constants.DEFAULT_HOST_LIST # FIXME: This should parse any arguments provided via -e or --extra-vars. Currently it seems # to parse the argument value wrongly and returns # '_raw_params': '<last character in variable>'. This prevents the ability to use # --extra-vars when executing plays. variable_manager = VariableManager() options = Options( verbosity=self.app.pargs.verbose, inventory=self.app.pargs.inventory, subset=self.app.pargs.limit, extra_vars=[self.app.pargs.extra_vars], forks=self.app.pargs.forks, ask_vault_pass=self.app.pargs.ask_vault_pass, vault_password_files=self.app.pargs.vault_password_file, tags=self.app.pargs.tags, skip_tags=self.app.pargs.skip_tags, become=self.app.pargs.become, become_method=self.app.pargs.become_method, become_user=self.app.pargs.become_user, become_ask_pass=self.app.pargs.ask_become_pass, ask_pass=self.app.pargs.ask_pass, private_key_file=self.app.pargs.private_key, remote_user=self.app.pargs.user, connection=self.app.pargs.connection, timeout=self.app.pargs.timeout, ssh_common_args=self.app.pargs.ssh_common_args, sftp_extra_args=self.app.pargs.sftp_extra_args, ssh_extra_args=self.app.pargs.ssh_extra_args, scp_extra_args=self.app.pargs.scp_extra_args, flush_cache=self.app.pargs.flush_cache, module_path=self.app.pargs.module_path) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.extra_vars = load_extra_vars(loader=loader, options=options) #variable_manager.options_vars = load_options_vars(options) if self.app.pargs.flush_cache: inventory.refresh_inventory() no_hosts = False if len(inventory.list_hosts()) == 0: print( Fore.YELLOW + "Warning! Provided hosts list is empty, only localhost is available." ) no_hosts = True # FIXME: Limit is currently ignored. inventory.subset(self.app.pargs.limit) if len(inventory.list_hosts()) == 0 and no_hosts is False: # Invalid limit print(Fore.RED + "Error! Specified --limit does not match any hosts.") sys.exit(1) play_data = loader.load_from_file(playbook_path) host_list = inventory.get_hosts( pattern=self.app.pargs.limit ) if self.app.pargs.limit else inventory.get_hosts() playbook = Playbook(loader=loader).load( playbook_path, variable_manager=variable_manager, loader=loader) pbex = PlaybookExecutor(playbooks=playbook.get_plays(), inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) # Ansible variables matching these strings will be excluded from the generated yaml content. ignored_elements = [ 'ansible_playbook_python', 'ansible_version', 'group_names', 'inventory_hostname', 'inventory_hostname_short', 'omit', 'playbook_dir', 'role_names' ] json_data = {} for host in host_list: host_vars = host.get_vars() host_name = host_vars[ 'ansible_host'] if 'ansible_host' in host_vars.keys( ) else host.get_name() for play in playbook.get_plays(): loader.set_vault_password(vault_pass) all_vars = variable_manager.get_vars(loader=loader, play=play, host=host) json_data[host_name] = all_vars['vars'] json_data[host_name]['ansible_groups'] = all_vars[ 'group_names'] json_data[host_name]['ansible_roles'] = all_vars['role_names'] for elem in ignored_elements: del json_data['{}'.format(host)][elem] json_data[host_name][ 'ansible_become_user'] = self.app.pargs.become_user if passwords['become_pass'] is not None: json_data[host_name]['ansible_become_pass'] = passwords[ 'become_pass'] json_data[host_name]['ansible_become'] = self.app.pargs.become json_data[host_name][ 'ansible_become_method'] = self.app.pargs.become_method if self.app.pargs.ssh_extra_args: json_data[host_name][ 'ansible_ssh_extra_args'] = self.app.pargs.ssh_extra_args if self.app.pargs.scp_extra_args: json_data[host_name][ 'ansible_scp_extra_args'] = self.app.pargs.scp_extra_args if self.app.pargs.sftp_extra_args: json_data[host_name][ 'ansible_sftp_extra_args'] = self.app.pargs.sftp_extra_args if self.app.pargs.ssh_common_args: json_data[host_name][ 'ansible_ssh_common_args'] = self.app.pargs.ssh_common_args json_data[host_name][ 'ansible_connection_timeout'] = self.app.pargs.timeout json_data[host_name][ 'ansible_connection_method'] = self.app.pargs.connection if self.app.pargs.private_key: json_data[host_name][ 'ansible_private_key'] = self.app.pargs.private_key json_data[host_name][ 'ansible_ask_pass'] = self.app.pargs.ask_pass if self.app.pargs.limit: json_data[host_name][ 'ansible_limit'] = self.app.pargs.limit # FIXME: Extra vars needs to be processed by Ansible instead of dumping them # here as a "dumb" string. if self.app.pargs.extra_vars: json_data[host_name][ 'ansible_extra_vars'] = self.app.pargs.extra_vars for key, value in json_data[host_name].iteritems(): if type(value) is AnsibleVaultEncryptedUnicode: json_data[host_name][key] = str(value) # Convert the processed python dictionary to a valid json string json_obj = json.dumps(json_data) # Take the json string, and convert it to a valid yaml string yml = YAML(typ="safe", pure=True) yml.default_flow_style = False yml_obj = yml.load(json_obj) if self.app.pargs.output is None: yml.dump(yml_obj, sys.stdout) else: with open(self.app.pargs.output, 'w') as outfile: yml.dump(yml_obj, outfile) print(Fore.GREEN + "Ansible variable dictionary written to {}".format( self.app.pargs.output)) sys.exit(1)
def _get_playbook_executor(self, variables, verbosity): Options = namedtuple('Options', [ 'connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'diff' ]) # -v given to us enables ansibles non-debug output. # So -vv should become ansibles -v. __main__.display.verbosity = max(0, verbosity - 1) # make sure ansible does not output warnings for our paternoster pseudo-play __main__._real_warning = __main__.display.warning def display_warning(msg, *args, **kwargs): if not msg.startswith('Could not match supplied host pattern'): __main__._real_warning(msg, *args, **kwargs) __main__.display.warning = display_warning loader = DataLoader() if ANSIBLE_VERSION < LooseVersion('2.4.0'): from ansible.inventory import Inventory variable_manager = VariableManager() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost,') variable_manager.set_inventory(inventory) else: from ansible.inventory.manager import InventoryManager inventory = InventoryManager(loader=loader, sources='localhost,') variable_manager = VariableManager(loader=loader, inventory=inventory) # force ansible to use the current python executable. Otherwise # it can end up choosing a python3 one (named python) or a different # python 2 version variable_manager.set_host_variable(inventory.localhost, 'ansible_python_interpreter', sys.executable) for name, value in variables: variable_manager.set_host_variable(inventory.localhost, name, value) pexec = PlaybookExecutor( playbooks=[self._playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=Options( connection='local', module_path=None, forks=1, listhosts=False, listtasks=False, listtags=False, syntax=False, become=None, become_method=None, become_user=None, check=False, diff=False, ), passwords={}, ) ansible.constants.RETRY_FILES_ENABLED = False if not verbosity: # ansible doesn't provide a proper API to overwrite this, # if you're using PlaybookExecutor instead of initializing # the TaskQueueManager (_tqm) yourself, like in the offical # example. pexec._tqm._stdout_callback = MinimalAnsibleCallback() return pexec
def __init__(self, resource, loader, variable_manager): self.resource = resource self.inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[]) self.dynamic_inventory()
global pending_results while pending_results > 0: debug("waiting for pending results (%d left)" % pending_results) _process_pending_results() time.sleep(0.01) debug("starting") cur_worker = 0 pending_results = 0 var_manager = VariableManager() debug("loading inventory") inventory = Inventory(host_list='/tmp/med_inventory', loader=loader, variable_manager=var_manager) hosts = inventory.get_hosts()[:] debug("done loading inventory") play_context = PlayContext() play_context.connection = 'local' for i in range(NUM_TASKS): #for j in range(NUM_HOSTS): for h in hosts: debug("queuing %s %d" % (h, i)) #h = Host(name="host%06d" % j) t = Task().load(dict(name="task %d" % (i,), debug="msg='hello from %s, %d'" % (h,i))) #t = Task().load(dict(name="task %d" % (i,), ping="")) #task_vars = var_manager.get_vars(loader=loader, host=h, task=t) task_vars = dict()
def run(self): super(ConsoleCLI, self).run() sshpass = None becomepass = None vault_pass = None # hosts if len(self.args) != 1: self.pattern = 'all' else: self.pattern = self.args[0] self.options.cwd = self.pattern # dynamically add modules as commands self.modules = self.list_modules() for module in self.modules: setattr( self, 'do_' + module, lambda arg, module=module: self.default(module + ' ' + arg)) setattr(self, 'help_' + module, lambda module=module: self.helpdefault(module)) self.normalize_become_options() (sshpass, becomepass) = self.ask_passwords() self.passwords = {'conn_pass': sshpass, 'become_pass': becomepass} self.loader = DataLoader() if self.options.vault_password_file: # read vault_pass from a file vault_pass = CLI.read_vault_password_file( self.options.vault_password_file, loader=self.loader) self.loader.set_vault_password(vault_pass) elif self.options.ask_vault_pass: vault_pass = self.ask_vault_passwords()[0] self.loader.set_vault_password(vault_pass) self.variable_manager = VariableManager() self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=self.options.inventory) self.variable_manager.set_inventory(self.inventory) no_hosts = False if len(self.inventory.list_hosts()) == 0: # Empty inventory no_hosts = True display.warning( "provided hosts list is empty, only localhost is available") self.inventory.subset(self.options.subset) hosts = self.inventory.list_hosts(self.pattern) if len(hosts) == 0 and not no_hosts: raise AnsibleError( "Specified hosts and/or --limit does not match any hosts") self.groups = self.inventory.list_groups() self.hosts = [x.name for x in hosts] # This hack is to work around readline issues on a mac: # http://stackoverflow.com/a/7116997/541202 if 'libedit' in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") else: readline.parse_and_bind("tab: complete") histfile = os.path.join(os.path.expanduser("~"), ".ansible-console_history") try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) self.set_prompt() self.cmdloop()
# -*- coding: utf-8 -*- import os import sys from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.executor.playbook_executor import PlaybookExecutor variable_manager = VariableManager() loader = DataLoader() inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/etc/ansible/hosts') playbook_path = '/etc/ansible/playbooks/test.yml' if not os.path.exists(playbook_path): print '[INFO] The playbook does not exist' sys.exit() Options = namedtuple('Options', [ 'listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check' ]) options = Options(listtags=False,
loader = DataLoader() options = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None, become_method=None, become_user=None, check=False) passwords = dict(vault_pass='******') # Instantiate our ResultCallback for handling results as they come in results_callback = ResultCallback() # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost') variable_manager.set_inventory(inventory) # create play with tasks play_source = dict( name="Ansible Play", hosts='localhost', gather_facts='no', tasks=[ dict(action=dict(module='shell', args='ls'), register='shell_out'), dict(action=dict(module='debug', args=dict( msg='{{shell_out.stdout}}'))) ]) play = Play().load(play_source, variable_manager=variable_manager,
def invoke_ansible_playbook(module_path, e_vars, playbook_path="site.yml", console=True): """ Invokes playbook """ loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = e_vars inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=['localhost']) passwords = {} utils.VERBOSITY = 4 Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check']) options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=module_path, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user='******', verbosity=utils.VERBOSITY, check=False) pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) if not console: cb = PlaybookCallback() pbex._tqm._stdout_callback = cb return_code = pbex.run() results = cb.results else: results = pbex.run() return results
module_name="lineinfile", module_args= ('dest={0} backup=yes line="{1}" owner=postgres regexp="{2}"' .format(pg_conf, l, regexp))).run() except Exception, e: log.error("Exception updating psql conf {0}: {1}".format(l, e)) # Restart psql misc.run("service postgresql restart") # Add required roles to the main Postgres server roles = ['scm', 'amon', 'rman', 'hive'] for role in roles: log.debug("Adding PostgreSQL role {0} (with pwd: {1})".format( role, self.db_pwd)) try: Runner( inventory=Inventory(['localhost']), transport='local', become=True, become_user='******', module_name="postgresql_user", module_args=( "name={0} role_attr_flags=LOGIN password={1}".format( role, self.db_pwd))).run() except Exception, e: log.error("Exception creating psql role {0}: {1}".format( role, e)) # Create required databases databases = ['scm', 'amon', 'rman', 'metastore'] for db in databases: owner = db if db == 'metastore':
def run(self): results = None super(InventoryCLI, self).run() # Initialize needed objects if getattr(self, '_play_prereqs', False): self.loader, self.inventory, self.vm = self._play_prereqs( self.options) else: # fallback to pre 2.4 way of initialzing from ansible.vars import VariableManager from ansible.inventory import Inventory self._new_api = False self.loader = DataLoader() self.vm = VariableManager() # use vault if needed if self.options.vault_password_file: vault_pass = CLI.read_vault_password_file( self.options.vault_password_file, loader=self.loader) elif self.options.ask_vault_pass: vault_pass = self.ask_vault_passwords() else: vault_pass = None if vault_pass: self.loader.set_vault_password(vault_pass) # actually get inventory and vars self.inventory = Inventory(loader=self.loader, variable_manager=self.vm, host_list=self.options.inventory) self.vm.set_inventory(self.inventory) if self.options.host: hosts = self.inventory.get_hosts(self.options.host) if len(hosts) != 1: raise AnsibleOptionsError( "You must pass a single valid host to --host parameter") myvars = self._get_host_variables(host=hosts[0]) self._remove_internal(myvars) # FIXME: should we template first? results = self.dump(myvars) elif self.options.graph: results = self.inventory_graph() elif self.options.list: top = self._get_group('all') if self.options.yaml: results = self.yaml_inventory(top) else: results = self.json_inventory(top) results = self.dump(results) if results: # FIXME: pager? display.display(results) exit(0) exit(1)
def __init__(self, resource): self.inventory = Inventory(host_list=[]) self.resource = resource self.gen_inventory()
def run_playbook(self, playbook_name, user_name, only_tags=None, skip_tags=None, extra_vars=None, subset=None): """ This function will launch a playbook. To handle logging, it will use stevedore which will load all extensions registered under the entrypoint cyclosible.plugins.storage. For example, it will let Cyclosible save his log on a file, on S3, or something else. :param playbook_name: :param user_name: :param only_tags: :param skip_tags: :param extra_vars: :return: """ history = PlaybookRunHistory.objects.create( playbook=Playbook.objects.get(name=playbook_name), date_launched=timezone.now(), status='RUNNING', task_id=self.request.id, launched_by=User.objects.get(username=user_name)) vault_password = None if settings.VAULT_ENABLED: try: self.mgr_vault = driver.DriverManager( namespace='cyclosible.plugins.vault', name=settings.VAULT_ENABLED, invoke_on_load=True, ) vault_password = self.mgr_vault.driver.get_password() except RuntimeError as e: logger.error(e) logger.debug('LOADED VAULT: {plugins} | Status: {status}'.format( plugins=settings.VAULT_ENABLED, status='OK' if vault_password else 'KO')) inventory = Inventory(vault_password=vault_password) # Here, we override the default ansible callbacks to pass our customs parameters stats = callbacks.AggregateStats() playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY, task_id=self.request.id) runner_cb = PlaybookRunnerCallbacks(stats=stats, verbose=utils.VERBOSITY, task_id=self.request.id) pb = ansible.playbook.PlayBook(playbook=''.join( [settings.PLAYBOOK_PATH, playbook_name, '.yml']), callbacks=playbook_cb, runner_callbacks=runner_cb, inventory=inventory, stats=stats, extra_vars=extra_vars, only_tags=only_tags, skip_tags=skip_tags, vault_password=vault_password, subset=subset) try: pb.run() hosts = sorted(pb.stats.processed.keys()) logger.info(hosts) playbook_cb.on_stats(pb.stats) history.status = 'SUCCESS' for h in hosts: t = pb.stats.summarize(h) display("%s : %s %s %s %s" % (hostcolor(h, t), colorize('ok', t['ok'], 'green'), colorize('changed', t['changed'], 'yellow'), colorize('unreachable', t['unreachable'], 'red'), colorize('failed', t['failures'], 'red')), task_id=self.request.id) except errors.AnsibleError as e: history.status = 'FAILED' display(u"ERROR: %s" % e, task_id=self.request.id) logger.error(u"ERROR: %s" % e) try: self.mgr_storage = enabled.EnabledExtensionManager( namespace='cyclosible.plugins.storage', check_func=check_plugin_enabled, invoke_on_load=True, invoke_kwds={'task_id': self.request.id}, verify_requirements=True) logger.debug('LOADED STORAGE: {plugins}'.format( plugins=', '.join(self.mgr_storage.names()))) except RuntimeError as e: logger.error(e) try: list_urls = [] self.mgr_storage.map(lambda ext: (ext.name, ext.obj.write_log())) urls = self.mgr_storage.map(lambda ext: (ext.name, ext.obj.get_url_log())) for url in urls: try: if url[1]: list_urls.append({url[0]: url[1]}) except IndexError: logger.debug('Index does not exist in the url returned') history.log_url = json.dumps(list_urls) except RuntimeError: logger.debug('No plugins available') history.date_finished = timezone.now() history.save() if history.status == 'FAILED': return 1
def empty_inventory(self): return Inventory(None)