示例#1
0
文件: runner.py 项目: qt-pay/chain
 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')
示例#2
0
文件: runner.py 项目: qt-pay/chain
 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)
示例#3
0
    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)
示例#4
0
文件: runner.py 项目: qt-pay/chain
    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)
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
文件: runner.py 项目: qt-pay/chain
    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()
示例#8
0
文件: runner.py 项目: qt-pay/chain
 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)