def playbook_run(self, group='test01', roles=['test01']): # create play with tasks play_source = dict(name="Ansible Playbook {}".format(roles), hosts=group, gather_facts='no', roles=roles) play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, # stdout_callback=self.results_callback, # Use our custom callback instead of the ``default`` callback plugin ) result = tqm.run(play) # print(result) finally: if tqm is not None: tqm.cleanup()
def order_run(self, group='test01', module_name='shell', module_args='hostname'): # create play with tasks play_source = dict( name="Ansible {} {}".format(module_name, module_args), hosts=group, gather_facts='no', tasks=[ dict(action=dict(module=module_name, args=module_args), register='shell_out'), # dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))) ]) play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, # stdout_callback=self.results_callback, # Use our custom callback instead of the ``default`` callback plugin ) result = tqm.run(play) # print(result) finally: if tqm is not None: tqm.cleanup()
def test_simple_alt_files(self): """Test one-level include with alternative tasks and variables""" play = Play.load(dict( name="test play", hosts=['foo'], gather_facts=False, tasks=[{'include_role': 'name=l3 tasks_from=alt defaults_from=alt'}]), loader=self.loader, variable_manager=self.var_manager) tasks = play.compile() for role, task_vars in self.get_tasks_vars(play, tasks): self.assertEqual(task_vars.get('l3_variable'), 'l3-alt') self.assertEqual(task_vars.get('test_variable'), 'l3-alt')
def run_cmd(self): passwords = { 'conn_pass': "******", 'become_pass': "******" } loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.pb_options) variable_manager.options_vars = load_options_vars(self.pb_options) inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.pb_options.inventory) variable_manager.set_inventory(inventory) play_ds = self._play_ds(self.pb_options.name, self.pb_options.pattern) play = Play().load(play_ds, variable_manager=variable_manager, loader=loader) self._tqm = None try: self._tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.pb_options, passwords=passwords, # stdout_callback=CallbackWrap(), ) rc = self._tqm.run(play) detail = json.loads(self._tqm._stdout_callback._dump_results) print("json.loads.self._tqm._stdout_callback._dump_results") print(json.loads(self._tqm._stdout_callback._dump_results)) print("self._tqm._stdout_callback dir") print(dir(self._tqm._stdout_callback)) finally: if self._tqm: self._tqm.cleanup() if loader: loader.cleanup_all_tmp_files() return {'rc': rc, 'detail': detail}
def test_nested_alt_files(self): """ Test nested includes with alternative tasks and variables. Variables from outer roles should be inherited, but overridden in inner roles. """ play = Play.load(dict(name="test play", hosts=['foo'], gather_facts=False, tasks=[{ 'include_role': 'name=l1 tasks_from=alt defaults_from=alt' }]), loader=self.loader, variable_manager=self.var_manager) tasks = play.compile() expected_roles = ['l1', 'l2', 'l3'] for role, task_vars in self.get_tasks_vars(play, tasks): expected_roles.remove(role) # Outer-most role must not have variables from inner roles yet if role == 'l1': self.assertEqual(task_vars.get('l1_variable'), 'l1-alt') self.assertEqual(task_vars.get('l2_variable'), None) self.assertEqual(task_vars.get('l3_variable'), None) self.assertEqual(task_vars.get('test_variable'), 'l1-alt') # Middle role must have variables from outer role, but not inner elif role == 'l2': self.assertEqual(task_vars.get('l1_variable'), 'l1-alt') self.assertEqual(task_vars.get('l2_variable'), 'l2-alt') self.assertEqual(task_vars.get('l3_variable'), None) self.assertEqual(task_vars.get('test_variable'), 'l2-alt') # Inner role must have variables from both outer roles elif role == 'l3': self.assertEqual(task_vars.get('l1_variable'), 'l1-alt') self.assertEqual(task_vars.get('l2_variable'), 'l2-alt') self.assertEqual(task_vars.get('l3_variable'), 'l3-alt') self.assertEqual(task_vars.get('test_variable'), 'l3-alt') else: self.fail() self.assertFalse(expected_roles)
def test_simple(self): """Test one-level include with default tasks and variables""" play = Play.load(dict(name="test play", hosts=['foo'], gather_facts=False, tasks=[{ 'include_role': 'name=l3' }]), loader=self.loader, variable_manager=self.var_manager) tasks = play.compile() tested = False for role, task_vars in self.get_tasks_vars(play, tasks): tested = True self.assertEqual(task_vars.get('l3_variable'), 'l3-main') self.assertEqual(task_vars.get('test_variable'), 'l3-main') self.assertTrue(tested)
def test_nested_alt_files(self): """ Test nested includes with alternative tasks and variables. Variables from outer roles should be inherited, but overridden in inner roles. """ play = Play.load(dict( name="test play", hosts=['foo'], gather_facts=False, tasks=[ {'include_role': 'name=l1 tasks_from=alt defaults_from=alt'} ] ), loader=self.loader, variable_manager=self.var_manager) tasks = play.compile() expected_roles = ['l1', 'l2', 'l3'] for role, task_vars in self.get_tasks_vars(play, tasks): expected_roles.remove(role) # Outer-most role must not have variables from inner roles yet if role == 'l1': self.assertEqual(task_vars.get('l1_variable'), 'l1-alt') self.assertEqual(task_vars.get('l2_variable'), None) self.assertEqual(task_vars.get('l3_variable'), None) self.assertEqual(task_vars.get('test_variable'), 'l1-alt') # Middle role must have variables from outer role, but not inner elif role == 'l2': self.assertEqual(task_vars.get('l1_variable'), 'l1-alt') self.assertEqual(task_vars.get('l2_variable'), 'l2-alt') self.assertEqual(task_vars.get('l3_variable'), None) self.assertEqual(task_vars.get('test_variable'), 'l2-alt') # Inner role must have variables from both outer roles elif role == 'l3': self.assertEqual(task_vars.get('l1_variable'), 'l1-alt') self.assertEqual(task_vars.get('l2_variable'), 'l2-alt') self.assertEqual(task_vars.get('l3_variable'), 'l3-alt') self.assertEqual(task_vars.get('test_variable'), 'l3-alt') else: self.fail() self.assertFalse(expected_roles)
def test_simple(self): """Test one-level include with default tasks and variables""" play = Play.load(dict( name="test play", hosts=['foo'], gather_facts=False, tasks=[ {'include_role': 'name=l3'} ] ), loader=self.loader, variable_manager=self.var_manager) tasks = play.compile() tested = False for role, task_vars in self.get_tasks_vars(play, tasks): tested = True self.assertEqual(task_vars.get('l3_variable'), 'l3-main') self.assertEqual(task_vars.get('test_variable'), 'l3-main') self.assertTrue(tested)
def run_play(play_ds, inventory_file, loader, remote_user, remote_pass, transport, extra_vars=None): variable_manager = get_variable_manager(loader) variable_manager.extra_vars = {} if extra_vars is None else extra_vars loader._FILE_CACHE = dict() # clean up previously read and cached files inv = get_inventory(loader=loader, variable_manager=variable_manager, inventory_file=inventory_file) variable_manager.set_inventory(inv) play = Play.load(play_ds, variable_manager=variable_manager, loader=loader) tqm = None result = 1 try: options = default_options(remote_user, transport) display.verbosity = options.verbosity tqm = TaskQueueManager( inventory=inv, variable_manager=variable_manager, loader=loader, options=options, passwords={'conn_pass': remote_pass} if remote_pass else {}, stdout_callback='minimal', run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS, run_tree=False, ) result = tqm.run(play) except Exception as ex: io.track_error('cannot run play', ex) finally: if tqm: tqm.cleanup() return result == 0
def test_nested(self): """ Test nested includes with default tasks and variables. Variables from outer roles should be inherited, but overriden in inner roles. """ play = Play.load(dict( name="test play", hosts=['foo'], gather_facts=False, tasks=[ {'include_role': 'name=l1'} ] ), loader=self.loader, variable_manager=self.var_manager) tasks = play.compile() for role, task_vars in self.get_tasks_vars(play, tasks): # Outer-most role must not have variables from inner roles yet if role == 'l1': self.assertEqual(task_vars.get('l1_variable'), 'l1-main') self.assertEqual(task_vars.get('l2_variable'), None) self.assertEqual(task_vars.get('l3_variable'), None) self.assertEqual(task_vars.get('test_variable'), 'l1-main') # Middle role must have variables from outer role, but not inner elif role == 'l2': self.assertEqual(task_vars.get('l1_variable'), 'l1-main') self.assertEqual(task_vars.get('l2_variable'), 'l2-main') self.assertEqual(task_vars.get('l3_variable'), None) self.assertEqual(task_vars.get('test_variable'), 'l2-main') # Inner role must have variables from both outer roles elif role == 'l3': self.assertEqual(task_vars.get('l1_variable'), 'l1-main') self.assertEqual(task_vars.get('l2_variable'), 'l2-main') self.assertEqual(task_vars.get('l3_variable'), 'l3-main') self.assertEqual(task_vars.get('test_variable'), 'l3-main')
def adhoc_run(self, hostip, module_name, module_args): self.callback = Adhoc_callback() play_source = dict( name="Ansible adhoc Play", hosts=hostip, gather_facts='no', tasks=[dict(action=dict( module=module_name, args=module_args, ))]) play = Play().load(play_source, variable_manager=self.commonobj.varobj, loader=self.commonobj.dataloader) tqm = None # if self.redisKey:self.callback = ModelResultsCollectorToSave(self.redisKey,self.logId) # else:self.callback = ModelResultsCollector() # self.callback = ModelResultsCollector() import traceback try: tqm = TaskQueueManager( inventory=self.commonobj.inventory, variable_manager=self.commonobj.varobj, loader=self.commonobj.dataloader, options=self.options, passwords=self.passwords, stdout_callback=self.callback, ) # tqm._stdout_callback = self.callback constants.HOST_KEY_CHECKING = False # 关闭第一次使用ansible连接客户端是输入命令 tqm.run(play) except Exception as err: print traceback.print_exc() # DsRedis.OpsAnsibleModel.lpush(self.redisKey,data=err) # if self.logId:AnsibleSaveResult.Model.insert(self.logId, err) finally: if tqm is not None: tqm.cleanup()
def get_playbook(self, *args, **kwargs): inject_ansible_paths(self.master.ctrl) from ansible.playbook import Play, Playbook import ansible.errors (options, loader, inventory, variable_manager) = self.get_ansible_variablemanager(**kwargs) playbooks_directory = get_playbooks_directory(self.master.main_config) playbook = kwargs.pop('playbook', None) if playbook is None: for instance_id in (self.uid, self.id): playbook_path = os.path.join(playbooks_directory, '%s.yml' % instance_id) if os.path.exists(playbook_path): playbook = playbook_path break if 'playbook' in self.config: if playbook is not None and playbook != self.config['playbook']: log.warning("Instance '%s' has the 'playbook' option set, but there is also a playbook at the default location '%s', which differs from '%s'." % (self.config_id, playbook, self.config['playbook'])) playbook = self.config['playbook'] if playbook is not None: log.info("Using playbook at '%s'." % playbook) roles = kwargs.pop('roles', None) if roles is None and 'roles' in self.config: roles = self.config['roles'] if roles is not None and playbook is not None: log.error("You can't use a playbook and the 'roles' options at the same time for instance '%s'." % self.config_id) sys.exit(1) if playbook is None and roles is None: return None skip_host_check = kwargs.pop('skip_host_check', False) try: if roles is None: pb = Playbook.load(playbook, variable_manager=variable_manager, loader=loader) plays = pb.get_plays() else: if isinstance(roles, basestring): roles = roles.split() data = { 'hosts': [self.uid], 'roles': roles} plays = [Play.load(data, variable_manager=variable_manager, loader=loader)] pb = Playbook(loader=loader) pb._entries.extend(plays) except ansible.errors.AnsibleError as e: log.error("AnsibleError: %s" % e) sys.exit(1) for play in plays: if play._attributes.get('remote_user') is None: play._attributes['remote_user'] = self.config.get('user', 'root') if self.config.get('sudo'): play._attributes['sudo'] = self.config.get('sudo') if not skip_host_check: hosts = play._attributes.get('hosts', None) if isinstance(hosts, basestring): hosts = hosts.split(':') if hosts is None: hosts = {} if self.uid not in hosts: log.warning("The host '%s' is not in the list of hosts (%s) of '%s'.", self.uid, ','.join(hosts), playbook) if not yesno("Do you really want to apply '%s' to the host '%s'?" % (playbook, self.uid)): sys.exit(1) play._attributes['hosts'] = [self.uid] return pb
def ansible_command(): # 获取主机组信息 BaseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) source = os.path.join(BaseDir, '/etc/ansible/hosts') # 实例化loader对象 loader = DataLoader() results_callback = ResultCallback() myinven = InventoryManager(loader=loader, sources=[ source, ]) # 实例化inventory对象 varmanager = VariableManager(loader=loader, inventory=myinven) # 实例化VariableManager对象 context.CLIARGS = ImmutableDict( connection='smart', module_path='/root/.ansible/plugins/modules', forks=10, become=None, become_method=None, become_user=None, check=False, diff=False) # 执行对象和模块 play_data = dict( name="Ansible adhoc example", hosts='all', gather_facts='no', tasks=[ dict(action=dict(module='setup', args=""), register='shell_out'), dict(action=dict(module='debug', args=dict( msg="{{ shell_out }}"))), ], ) play = Play().load(data=play_data, loader=loader, variable_manager=varmanager) passwords = {} tqm = None try: tqm = TaskQueueManager( inventory=myinven, variable_manager=varmanager, loader=loader, passwords=passwords, stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout ) result = tqm.run( play ) # most interesting data for a play is actually sent to the callback's methods finally: # we always need to cleanup child procs and the structres we use to communicate with them if tqm is not None: list = [] dict2 = {} dict2.update(tqm.__dict__) list.append(dict2) dict3 = dict2['hostvars'] list_1 = [] for v in dict3: ip = v list_1.append(ip) for j in list_1: dict4 = dict3[j]['ansible_facts'] for k in dict4: print(k) if k == 'hostname': print(dict4['hostname']) elif k == 'fqdn': print(dict4['fqdn']) elif k == 'uptime_seconds': print(dict4['uptime_seconds']) elif k == 'domain': print(dict4['domain']) elif k == 'memtotal_mb': print(str(dict4['memtotal_mb'] / 1024) + 'M') elif k == 'default_ipv4': print(dict4['default_ipv4']['address'])
def _install(self, ip_address): # initialize needed objects # self.variable_manager_initialization(ip_address) host_list = [ip_address] hosts = ','.join(host_list) if len(host_list) == 1: hosts += ',' package = [self.package] # create data structure that represents our play # including tasks, this is basically what our YAML loader does internally. print(f'hosts : {ip_address} \n roles : {package}') play_source = { 'hosts': hosts, 'gather_facts': True, 'become': True, 'become_user': '******', 'become_method': 'sudo', 'roles': package } passwords = dict() results_callback = ResultsCollectorJSONCallback() tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, passwords=passwords, stdout_callback=results_callback, ) # Create play object, playbook objects use .load instead of init or new methods, # this will also automatically create the task objects from the info provided in play_source play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader, vars={'ansible_become_pass': '******'}) # Actually run it try: result = tqm.run( play ) # most interesting data for a play is actually sent to the callback's methods finally: # we always need to cleanup child procs and the structures we use to communicate with them tqm.cleanup() if self.loader: self.loader.cleanup_all_tmp_files() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True) print("UP ***********") for host, result in results_callback.host_ok.items(): print('{0} >>> {1}'.format(host, result._result)) print("FAILED *******") for host, result in results_callback.host_failed.items(): print('{0} >>> {1}'.format(host, result._result['msg'])) print("DOWN *********") for host, result in results_callback.host_unreachable.items(): print('{0} >>> {1}'.format(host, result._result['msg']))
def execute(self, *args, **kwargs): """ Puts args and kwargs in a way ansible can understand. Calls ansible and interprets the result. """ assert self.is_hooked_up, "the module should be hooked up to the api" print("============") print(args) print("============") print(kwargs) self.module_args = module_args = self.get_module_args(args, kwargs) print("=========") print(module_args) loader = DataLoader() variable_manager = VariableManager() variable_manager.extra_vars = self.api.options.extra_vars # Ansible has some support for host lists, but it assumes at times # that these host lists are not in fact lists but a string pointing # to a host list file. The easiest way to make sure that Ansible gets # what we want is to pass a host list as a string which always contains # at least one comma so that ansible knows it's a string of hosts. host_list = ','.join(self.api.servers) + ',' inventory = UncachedInventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory) play_source = { 'name': "Suitable Play", 'hosts': self.api.servers, 'gather_facts': 'no', 'tasks': [{ 'action': { 'module': self.module_name, 'args': module_args } }] } play = Play.load(play_source, variable_manager=variable_manager, loader=loader) log.info(u'running {}'.format(u'- {module_name}: {module_args}'.format( module_name=self.module_name, module_args=module_args))) start = datetime.utcnow() task_queue_manager = None callback = SilentCallbackModule() try: task_queue_manager = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=self.api.options, passwords=getattr(self.api.options, 'passwords', {}), stdout_callback=callback) task_queue_manager.run(play) finally: if task_queue_manager is not None: task_queue_manager.cleanup() log.info(u'took {} to complete'.format(datetime.utcnow() - start)) return self.evaluate_results(callback)
def executor(self): # data là dang dict loader = DataLoader() # load inventory inventories = AnsibleInventoryManager(loader=loader, sources=[self.inventory]) # load variable manager variable_manager = AnsibleVariableManager(loader=loader, inventory=inventories) # result callback về results_callback = AnsibleResultsCallback() options = AnsibleOption() # load ansible option # pase option from data # [inventory, module, group, args] for k_option, v_option in self.data.iteritems(): if k_option not in ["inventory", "module", "group", "args"]: # add option try: if k_option == "listhosts": data_out_put = inventories.get_groups_dict()[ self.data["group"]] return APIResponse(data_out_put, 201) else: options.__setattr__(k_option, True) except: pass play_source = dict(name="Ansible Play Module " + self.data["module"] + " " + self.data["args"], hosts=str(self.data["group"]), gather_facts='no', tasks=[ dict(action=dict(module=self.data["module"], args=self.data["args"])), ]) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None try: tqm = TaskQueueManager( inventory=inventories, variable_manager=variable_manager, loader=loader, options=options, passwords=None, stdout_callback= results_callback, # Use our custom callback instead of the ``default`` callback plugin ) tqm.run(play) finally: if tqm is not None: tqm.cleanup() return results_callback.return_data