def __check(self): if self.options.playbook_path is None or \ not os.path.exists(self.options.playbook_path): raise AnsibleError("Not Found the playbook file: {}.".format( self.options.playbook_path)) if not self.inventory.list_hosts('all'): raise AnsibleError('Inventory is empty')
def check_pattern(self, pattern): if not pattern: raise AnsibleError("Pattern `{}` is not valid!".format(pattern)) if not self.inventory.list_hosts("all"): raise AnsibleError("Inventory is empty.") if not self.inventory.list_hosts(pattern): raise AnsibleError("pattern: %s dose not match any hosts." % pattern)
def execute(self, cmd, pattern='all', module='shell', uuid='uuid'): if module and module not in self.modules_choices: raise AnsibleError("Module should in {}".format( self.modules_choices)) tasks = [{"action": {"module": module, "args": cmd}}] return self.run(tasks, pattern, play_name=cmd, uuid=uuid)
def execute(self, cmd, pattern, module=None): if module and module not in self.modules_choices: raise AnsibleError("Module should in {}".format( self.modules_choices)) else: module = "shell" tasks = [{"action": {"module": module, "args": cmd}}] hosts = self.inventory.get_hosts(pattern=pattern) name = "Run command {} on {}".format( cmd, ", ".join([host.name for host in hosts])) return self.run(tasks, pattern, play_name=name)
def run(self, tasks, pattern, play_name='Ansible Ad-hoc', gather_facts='no', file_obj=None): self.results_callback = self.get_result_callback(file_obj) play_source = dict(name=play_name, hosts=pattern, gather_facts=gather_facts, tasks=tasks) 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, stdout_callback=self.results_callback, passwords=self.options.passwords, ) try: result = tqm.run(play) # return self.results_callback except Exception as e: raise AnsibleError(e) finally: tqm.cleanup() self.loader.cleanup_all_tmp_files() results_raw = {} results_raw['success'] = {} results_raw['failed'] = {} results_raw['unreachable'] = {} for host, result in self.results_callback.host_ok.items(): results_raw['success'][host] = json.dumps(result._result) for host, result in self.results_callback.host_failed.items(): results_raw['failed'][host] = result._result['msg'] for host, result in self.results_callback.host_unreachable.items(): results_raw['unreachable'][host] = result._result['msg'] print(results_raw)
def run(self, tasks, pattern, play_name='Ansible Ad-hoc', gather_facts='no', uuid='uuid'): """ :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ] :param pattern: all, *, or others :param play_name: The play name :param gather_facts: :return: """ self.check_pattern(pattern) self.results_callback = self.get_result_callback() cleaned_tasks = self.clean_tasks(tasks) context.CLIARGS = ImmutableDict(self.options) play_source = dict(name=play_name, hosts=pattern, gather_facts=gather_facts, tasks=cleaned_tasks) 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, stdout_callback=self.results_callback, # passwords=dict(vault_pass='******'), passwords={"conn_pass": self.options.get("password", "")}) try: tqm.run(play) return self.results_callback except Exception as e: raise AnsibleError(e) finally: if tqm is not None: tqm.cleanup() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
def run(self, tasks, pattern, play_name='Ansible Ad-hoc', gather_facts='no'): """ :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ] :param pattern: all, *, or others :param play_name: The play name :return: """ self.check_pattern(pattern) results_callback = self.results_callback_class() cleaned_tasks = self.clean_tasks(tasks) play_source = dict(name=play_name, hosts=pattern, gather_facts=gather_facts, tasks=cleaned_tasks) 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, stdout_callback=results_callback, passwords=self.options.passwords, ) try: tqm.run(play) return results_callback except Exception as e: raise AnsibleError(e) finally: tqm.cleanup() self.loader.cleanup_all_tmp_files()
def check_module_args(module_name, module_args=''): if module_name in C.MODULE_REQUIRE_ARGS and not module_args: err = "No argument passed to '%s' module." % module_name raise AnsibleError(err)