Python ansible.executor.task_queue_manager.TaskQueueManager() Examples

The following are 14 code examples of ansible.executor.task_queue_manager.TaskQueueManager(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ansible.executor.task_queue_manager , or try the search function .
Example #1
Source File: ansible_executor_v2.py    From im with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, playbook, inventory, variable_manager,
                 loader, options, passwords, output):
        self._playbook = playbook
        self._inventory = inventory
        self._variable_manager = variable_manager
        self._loader = loader
        self._options = options
        self.passwords = passwords
        self._unreachable_hosts = dict()

        try:
            self._tqm = TaskQueueManager(inventory=inventory,
                                         variable_manager=variable_manager,
                                         loader=loader, options=options,
                                         passwords=self.passwords,
                                         stdout_callback=AnsibleCallbacks(output))
        except TypeError:
            # In case of ansible 2.8 options parameter has been removed
            self._tqm = TaskQueueManager(inventory=inventory,
                                         variable_manager=variable_manager,
                                         loader=loader,
                                         passwords=self.passwords,
                                         stdout_callback=AnsibleCallbacks(output)) 
Example #2
Source File: core.py    From i3-xfce with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _execute_play(play_source, inventory, var_mgr, loader, options, callback):  # pylint: disable=too-many-arguments
    """
    Execute the playbook
    """
    play = Play().load(play_source, variable_manager=var_mgr, loader=loader)
    tqm = None
    try:
      tqm = TaskQueueManager(
          inventory=inventory,
          variable_manager=var_mgr,
          loader=loader,
          options=options,
          passwords=None,
          stdout_callback=callback,
          )
      _ = tqm.run(play)
    except Exception as exc:
      raise TaskExecutionException(str(exc))
    finally:
      if tqm is not None:
        tqm.cleanup() 
Example #3
Source File: ansible_task.py    From heartbeats with MIT License 6 votes vote down vote up
def run(self):
        # actually run it
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=dict(),
            )
            tqm._stdout_callback = self.call_back
            result = tqm.run(self.play)
            self._summary = self.call_back.summary
            self._results = self.call_back.results
            return result
        finally:
            if tqm is not None:
                tqm.cleanup() 
Example #4
Source File: playbook.py    From DevOps with GNU General Public License v2.0 6 votes vote down vote up
def run(self):
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory = self.inventory,
                variable_manager = self.variable_manager,
                loader = self.loader,
                options = self.options,
                passwords = {},
                stdout_callback = self.stdout_callback
            )
            for p in self.play:
                result = tqm.run(p)
            self.delete_key()
        finally:
            if tqm is not None:
                tqm.cleanup() 
Example #5
Source File: playbook.py    From ceph-ansible-copilot with GNU Lesser General Public License v2.1 6 votes vote down vote up
def run(self):
        # running the playbook
        tqm = None
        try:
            tqm = TaskQueueManager(
                    inventory=self.inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    options=self.options,
                    passwords={},
                    # stdout_callback="default",
                    stdout_callback=self.callback,
                )
            self.rc = tqm.run(self.playbook)

        finally:
            if tqm is not None:
                tqm.cleanup() 
Example #6
Source File: ansible_api.py    From swallow with MIT License 5 votes vote down vote up
def run_adhoc(self, hosts, module_name, module_args=''):
        ###########################
        # run module from andible ad-hoc.
        ##########################
        play_source = dict(
            name="Ansible Play",
            hosts=hosts,
            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)

        # 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.callback,
            )
            tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup() 
Example #7
Source File: ansible_api.py    From imoocc with GNU General Public License v2.0 5 votes vote down vote up
def run_model(self, host_list, module_name, module_args):
        """
        run module from andible ad-hoc.
        module_name: ansible module_name
        module_args: ansible module args
        """
        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 = 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.inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    options=self.options,
                    passwords=self.passwords,
                    stdout_callback = "minimal",
            )
            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() 
Example #8
Source File: runner.py    From KubeOperator with Apache License 2.0 4 votes vote down vote up
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
        :param gather_facts:
        :return:
        """
        self.check_pattern(pattern)
        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=self.results_callback,
            passwords=self.options.passwords,
        )
        msg = ("Get matched hosts: {}".format(
            self.inventory.get_matched_hosts(pattern)
        ))
        try:
            tqm.send_callback('on_playbook_start', play.name)
            self.results_callback.display(msg)
            tqm.run(play)
            return self.results_callback.results
        except Exception as e:
            raise AnsibleError(e)
        finally:
            tqm.send_callback('v2_playbook_on_stats', tqm._stats)
            tqm.send_callback('on_playbook_end', play.name)
            tqm.cleanup()
            self.loader.cleanup_all_tmp_files() 
Example #9
Source File: adhoc.py    From lykops with Apache License 2.0 4 votes vote down vote up
def run(self, inventory_content, pattern='all'):
        
        '''
        运行adhoc
        '''
        
        self.pattern = pattern
        self.inventory_content = inventory_content
        if not self.options.module_name :
            self.logger.error(self.log_prefix + '准备工作失败,原因:执行模块不能为空')
            return (False, '执行模块不能为空,请输入模块名')
        else: 
            if self.options.module_name in C.MODULE_REQUIRE_ARGS and not self.options.module_args :
                self.logger.error(self.log_prefix + '准备工作失败,原因:执行模块参数为空')
                return (False, '执行模块参数为空,请输入模块参数')

        for name, obj in get_all_plugin_loaders():
            name = name
            if obj.subdir:
                plugin_path = os.path.join('.', obj.subdir)
                if os.path.isdir(plugin_path):
                    obj.add_directory(plugin_path)
        
        self._gen_tasks()

        play = Play().load(self.tasks_dict, variable_manager=self.variable_manager, loader=self.loader)

        try :
            self.host_list = self.inventory.list_hosts(self.pattern)
        except :
            self.host_list = []
            
        if len(self.host_list) == 0 :
            self.logger.error(self.log_prefix + '准备工作失败,原因:没有匹配主机名')
            return (False, '执行失败,没有匹配主机名')
        
        self._loading_callback()
        
        self._tqm = None
        try:
            self._tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords,
                stdout_callback=self.callback,
                # run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                # run_tree=False,
            )
            self._tqm.run(play)
        finally:
            if self._tqm:
                self._tqm.cleanup()
                
            if self.loader:
                self.loader.cleanup_all_tmp_files()

        self.logger.info(self.log_prefix + '发送成功')
        return True 
Example #10
Source File: api_runner.py    From column with GNU General Public License v3.0 4 votes vote down vote up
def run_module(self, module_name='ping', module_args=None, hosts="all",
                   inventory_file=None, **kwargs):

        if not module_args:
            check_raw = module_name in ('command', 'win_command', 'shell',
                                        'win_shell', 'script', 'raw')
            module_args = parse_kv(constants.DEFAULT_MODULE_ARGS, check_raw)

        conn_pass = None
        if 'conn_pass' in kwargs:
            conn_pass = kwargs['conn_pass']

        become_pass = None
        if 'become_pass' in kwargs:
            become_pass = kwargs['become_pass']

        passwords = {'conn_pass': conn_pass, 'become_pass': become_pass}

        options = self._build_opt_dict(inventory_file, **kwargs)

        variable_manager = vars.VariableManager()
        loader = dataloader.DataLoader()
        variable_manager.extra_vars = options.extra_vars

        ansible_inv = inventory.Inventory(loader=loader,
                                          variable_manager=variable_manager,
                                          host_list=options.inventory)
        variable_manager.set_inventory(ansible_inv)
        ansible_inv.subset(options.subset)

        play_ds = self._play_ds(hosts, module_name, module_args)
        play_obj = play.Play().load(play_ds, variable_manager=variable_manager,
                                    loader=loader)

        try:
            tqm = task_queue_manager.TaskQueueManager(
                inventory=ansible_inv,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                stdout_callback='minimal',
                run_additional_callbacks=True
            )

            # There is no public API for adding callbacks, hence we use a
            # private property to add callbacks
            tqm._callback_plugins.extend(self._callbacks)

            result = tqm.run(play_obj)
        finally:
            if tqm:
                tqm.cleanup()
            if loader:
                loader.cleanup_all_tmp_files()

        stats = tqm._stats
        result = self._process_stats(stats)
        return result 
Example #11
Source File: runner.py    From chain with Apache License 2.0 4 votes vote down vote up
def run(
            self,
            tasks,
            pattern,
            play_name='Ansible Ad-hoc',
            gather_facts='no',):
        """
        :param gather_facts:
        :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() 
Example #12
Source File: runner.py    From devops with GNU General Public License v3.0 4 votes vote down vote up
def run(
            self,
            tasks,
            pattern,
            play_name='Ansible Ad-hoc',
            gather_facts='no',):
        """
        :param gather_facts:
        :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() 
Example #13
Source File: ansible_playbook.py    From kalliope with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, **kwargs):
        super(Ansible_playbook, self).__init__(**kwargs)

        self.task_file = kwargs.get('task_file', None)
        self.sudo = kwargs.get('sudo', False)
        self.sudo_user = kwargs.get('sudo_user', False)
        self.sudo_password = kwargs.get('sudo_password', False)

        # check if parameters have been provided
        if self._is_parameters_ok():
            # since the API is constructed for CLI it expects certain options to always be set in the context object
            context.CLIARGS = self._get_options()

            # initialize needed objects
            loader = DataLoader()

            passwords = {'become_pass': self.sudo_password}

            inventory = InventoryManager(loader=loader, sources="localhost,")

            # variable manager takes care of merging all the different sources to give you a unified
            # view of variables available in each context
            variable_manager = VariableManager(loader=loader, inventory=inventory)
            variable_manager.set_inventory(inventory)

            playbooks = None
            with open(self.task_file, 'r') as stream:
                try:
                    playbooks = yaml.full_load(stream)
                except yaml.YAMLError as exc:
                    logger.debug("Ansibe playbook error: {}".format(exc))

            if playbooks is not None:
                # force usage of python 3 interpreter
                playbooks[0].setdefault("vars", {})
                playbooks[0]["vars"]["ansible_python_interpreter"] = "/usr/bin/python3"

                play = Play().load(playbooks[0], variable_manager=variable_manager, loader=loader)

                # Run it - instantiate task queue manager, which takes care of forking and setting up all objects
                # to iterate over host list and tasks
                tqm = None
                try:
                    tqm = TaskQueueManager(
                        inventory=inventory,
                        variable_manager=variable_manager,
                        loader=loader,
                        passwords=passwords,
                        stdout_callback='default',
                        # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
                    )
                    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:
                        tqm.cleanup() 
Example #14
Source File: runner.py    From autoops with Apache License 2.0 4 votes vote down vote up
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()