Exemple #1
0
 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')
Exemple #2
0
 def check_pattern(self, pattern):  #校验host_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)
Exemple #3
0
    def execute(self, cmd, pattern="all", 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)
Exemple #4
0
    def run(self,
            tasks,
            pattern="all",
            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
        :param gather_facts:是否收集系统的参数
        :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()
Exemple #5
0
 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)