def run_model(self, host_list, module_name, module_args): """ ansible group1 -m shell -a 'ls /tmp' """ print(" in run model") self.callback = ADhocCallback() play_source = dict( name="Ansible Play", hosts=host_list, gather_facts='no', tasks=[dict(action=dict(module=module_name, args=module_args))]) play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader) tqm = TaskQueueManager( inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=self.passwords, stdout_callback="minimal", ) print(tqm) print(dir(play)) print(play.get_tasks()) print(play.get_name()) print(play.get_vars()) print(play.get_roles()) try: tqm._stdout_callback = self.callback constants.HOST_KEY_CHECKING = False # 关闭第一次使用ansible连接客户端是输入命令 tqm.run(play) self.results_raw = self.callback.result_row print("[2] %s" % self.results_raw) # debug 2 except Exception as err: print(err) finally: if tqm is not None: tqm.cleanup()