def ansible_module_environment_set(args={}, complex_args={}): """Modifies ansible.module_utils.basic with the parameters of the module""" import ansible.module_utils.basic as basic from ansible import __version__ if hasattr(basic, "VERSION"): backup_version = basic.VERSION backup_args = basic.MODULE_ARGS backup_complex_args = basic.MODULE_COMPLEX_ARGS if hasattr(basic, "VERSION"): basic.VERSION = __version__ basic.MODULE_ARGS = serialize_args(args) basic.MODULE_COMPLEX_ARGS = json.dumps(complex_args) yield if hasattr(basic, "VERSION"): basic.VERSION = backup_version basic.MODULE_ARGS = backup_args basic.MODULE_COMPLEX_ARGS = backup_complex_args
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): def not_omit_token(value): return value != self.runner.omit_token ( sources_complex_args_list, passthru_complex_args_map ) = self._partition_options(complex_args) sources_complex_args_map = self._make_sources_map(sources_complex_args_list) sources_complex_args_map = self._filter_recursive(not_omit_token, sources_complex_args_map) passthru_complex_args_map = self._filter_recursive(not_omit_token, passthru_complex_args_map) ( sources_module_args_hash_list, passthru_module_args_hash ) = self._partition_options(utils.parse_kv(module_args)) sources_module_args_map = self._make_sources_map(sources_module_args_hash_list) sources_module_args_map = self._filter_recursive(not_omit_token, sources_module_args_map) passthru_module_args_hash = self._filter_recursive(not_omit_token, passthru_module_args_hash) sources_options_map = utils.merge_hash(sources_complex_args_map, sources_module_args_map) passthru_options_map = utils.merge_hash(passthru_complex_args_map, passthru_module_args_hash) skip_action_plugin = utils.boolean(passthru_options_map.get('skip_action_plugin', False)) try: del(passthru_options_map['skip_action_plugin']) except KeyError: pass passthru_options_keys = passthru_options_map.keys() if len(passthru_options_keys) > 1: raise errors.AnsibleError("Only one module can be run at a time; saw modules: %s" % ', '.join(passthru_options_keys)) # Iterate over 'copy' files for src, options in sources_options_map.iteritems(): # Construct remote filesystem path dest = options.get('dest', None) if dest is None: if tmp is None: tmp = self.runner._make_tmp_path(conn) dest = tmp # Interpret relative paths as starting with the remote tmp # directory elif not dest.startswith('/'): if tmp is None: tmp = self.runner._make_tmp_path(conn) os.path.join(tmp, dest) copy_module_args_hash = sources_module_args_map.get(src, {}) copy_module_args_hash.update(dict(dest=dest)) copy_module_args = utils.serialize_args(copy_module_args_hash) copy_complex_args = sources_complex_args_map.get(src, None) # Copy source to destination. # # XXX because the 'copy' action_plugin doesn't pass through # persist_files or delete_remote_tmp, we need to make a temporary # adjustment to C.DEFAULT_KEEP_REMOTE_FILES. The 'as' clause is # necessary in order to affect C.DEFAULT_KEEP_REMOTE_FILES in the # scope of ansible.runner. return_data = None with tmp_keep_remote_files(True) as C.DEFAULT_KEEP_REMOTE_FILES: return_data = self._copy(conn, tmp, 'copy', copy_module_args, inject, complex_args=copy_complex_args) # Fail here if files weren't copied over correctly if not return_data.is_successful(): return return_data, 'copy', copy_module_args, copy_complex_args for passthru_module_name, passthru_options in passthru_options_map.iteritems(): passthru_complex_args = passthru_options_map.get(passthru_module_name, None) passthru_module_args = utils.serialize_args(passthru_module_args_hash) # Handle things like 'command: do_something' if not isinstance(passthru_complex_args, dict): if isinstance(passthru_complex_args, basestring): passthru_module_args = passthru_complex_args passthru_complex_args = None # Instantiate the action_plugin for the wanted module return_data = None if not skip_action_plugin: passthru_handler = utils.plugins.action_loader.get(passthru_module_name, self.runner) if passthru_handler: try: return_data = passthru_handler.run(conn, tmp, passthru_module_name, passthru_module_args, inject, complex_args=passthru_complex_args, **kwargs) except Exception as err: return_data = ReturnData(conn=conn, result=dict(failed=True, msg="Encountered error in %s module: %s" % (passthru_module_name, str(err)))) else: try: return_data = self.runner._execute_module(conn, tmp, passthru_module_name, passthru_module_args, inject=inject, complex_args=passthru_complex_args, **kwargs) except Exception as err: return_data = ReturnData(conn=conn, result=dict(failed=True, msg="Encountered error in %s module: %s" % (passthru_module_name, str(err)))) return return_data, passthru_module_name, passthru_module_args, passthru_complex_args