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
def process(ctx, params): playbook_path = params[ "playbook_path"] if "playbook_path" in params else "site.yml" inventory_path = params[ "inventory_path"] if "inventory_path" in params else "inventory/hosts" connection = params["connection"] if "connection" in params else "local" become = params["become"] if "become" in params else False become_method = params[ "become_method"] if "become_method" in params else None become_user = params["become_user"] if "become_user" in params else None extra_vars = dict(ctx) if "extra_vars" in params: extra_vars.update(params["extra_vars"]) if "ansible_python_interpreter" not in params["extra_vars"]: extra_vars.update( {"ansible_python_interpreter": "/opt/app-root/bin/python"} ) # Use the Python that we're running as by default, so dependencies are available loader = DataLoader() passwords = dict(vault_pass=params["vault_password"] if "vault_password" in params else "") context.CLIARGS = ImmutableDict(connection=connection, module_path=["./library"], forks=10, become=become, become_method=become_method, become_user=become_user, check=False, diff=False) # results_callback = ResultCallback() inventory = InventoryManager(loader=loader, sources=[inventory_path]) variable_manager = VariableManager(loader=loader, inventory=inventory) tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=passwords, # stdout_callback=results_callback, # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout ) with open(playbook_path, 'r') as file: playbook_str = file.read() playbook_data = yaml.load(playbook_str, Loader=yaml.FullLoader) plays = [ play.Play().load(data=play_data, variable_manager=variable_manager, loader=loader, vars=extra_vars) for play_data in playbook_data ] try: for play_data in plays: result = tqm.run(play_data) finally: if tqm is not None: tqm.cleanup()
def _run_play(self, play_source, host_vars): host_list = play_source['hosts'] loader = dataloader.DataLoader() # FIXME(jpena): we need to behave differently if we are using # Ansible >= 2.4.0.0. Remove when only versions > 2.4 are supported if PRE_24_ANSIBLE: variable_manager = VariableManager() inventory_inst = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory_inst) else: inventory_inst = Inventory(loader=loader, sources=','.join(host_list) + ',') variable_manager = VariableManager(loader=loader, inventory=inventory_inst) for host, variables in host_vars.items(): host_inst = inventory_inst.get_host(host) for var_name, value in variables.items(): if value is not None: variable_manager.set_host_variable( host_inst, var_name, value) storage = [] callback = MyCallback(storage) tqm = task_queue_manager.TaskQueueManager( inventory=inventory_inst, variable_manager=variable_manager, loader=loader, options=self.options, passwords=self.passwords, stdout_callback=callback, ) # create play play_inst = play.Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it try: tqm.run(play_inst) finally: tqm.cleanup() return storage
def _run_play(self, play_source, host_vars): host_list = play_source['hosts'] loader = dataloader.DataLoader() variable_manager = VariableManager() inventory_inst = inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory_inst) for host, variables in host_vars.items(): host_inst = inventory_inst.get_host(host) for var_name, value in variables.items(): if value is not None: variable_manager.set_host_variable(host_inst, var_name, value) storage = [] callback = MyCallback(storage) tqm = task_queue_manager.TaskQueueManager( inventory=inventory_inst, variable_manager=variable_manager, loader=loader, options=self.options, passwords=self.passwords, stdout_callback=callback, ) # create play play_inst = play.Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it try: tqm.run(play_inst) finally: tqm.cleanup() return storage
def _run_play(self, play_source): host_list = play_source['hosts'] loader = dataloader.DataLoader() variable_manager = VariableManager() inventory_inst = inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) variable_manager.set_inventory(inventory_inst) passwords = dict(vault_pass='******') # create play play_inst = play.Play().load(play_source, variable_manager=variable_manager, loader=loader) storage = [] callback = MyCallback(storage) # actually run it tqm = None try: tqm = task_queue_manager.TaskQueueManager( inventory=inventory_inst, variable_manager=variable_manager, loader=loader, options=self.options, passwords=passwords, stdout_callback=callback, ) tqm.run(play_inst) finally: if tqm is not None: tqm.cleanup() return storage
def run_play(pattern_hosts, play_source, inventory_path=None, extra_vars=None, on_error_continue=False): """Run a play. Args: pattern_hosts (str): pattern to describe ansible hosts to target. see https://docs.ansible.com/ansible/latest/intro_patterns.html play_source (dict): ansible task inventory_path (str): inventory to use extra_vars (dict): extra_vars to use on_error_continue(bool): Don't throw any exception in case a host is unreachable or the playbooks run with errors Raises: :py:class:`enoslib.errors.EnosFailedHostsError`: if a task returns an error on a host and ``on_error_continue==False`` :py:class:`enoslib.errors.EnosUnreachableHostsError`: if a host is unreachable (through ssh) and ``on_error_continue==False`` Returns: List of all the results """ # NOTE(msimonin): inventory could be infered from a host list (maybe) results = [] inventory, variable_manager, loader, options = _load_defaults( inventory_path, extra_vars=extra_vars) callback = _MyCallback(results) passwords = {} tqm = task_queue_manager.TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=callback) # create play play_inst = play.Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it try: tqm.run(play_inst) finally: tqm.cleanup() # Handling errors failed_hosts = [] unreachable_hosts = [] for r in results: if r.status == STATUS_UNREACHABLE: unreachable_hosts.append(r) if r.status == STATUS_FAILED: failed_hosts.append(r) if len(failed_hosts) > 0: logger.error("Failed hosts: %s" % failed_hosts) if not on_error_continue: raise EnosFailedHostsError(failed_hosts) if len(unreachable_hosts) > 0: logger.error("Unreachable hosts: %s" % unreachable_hosts) if not on_error_continue: raise EnosUnreachableHostsError(unreachable_hosts) return results
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) # dynamically load any plugins get_all_plugin_loaders() loader = dataloader.DataLoader() inventory = InventoryManager(loader=loader, sources=options.inventory) # create the variable manager, which will be shared throughout # the code, ensuring a consistent view of global variables variable_manager = VariableManager(loader=loader, inventory=inventory) options.extra_vars = { six.u(key): six.u(value) for key, value in options.extra_vars.items() } variable_manager.extra_vars = cli.load_extra_vars(loader, options) inventory.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=inventory, 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