diff --git a/changelogs/fragments/307_remote_src.yml b/changelogs/fragments/307_remote_src.yml new file mode 100644 index 00000000..cbc8863e --- /dev/null +++ b/changelogs/fragments/307_remote_src.yml @@ -0,0 +1,3 @@ +minor_changes: +- k8s - add parameter to specify if src file is located on remote node and not on Ansible Controller (https://github.com/ansible-collections/community.kubernetes/issues/307). +- k8s - add parameter to specify if kubeconfig file is located on remote node and not on Ansible Controller. diff --git a/plugins/action/k8s_info.py b/plugins/action/k8s_info.py index 6b26225f..b28c69fa 100644 --- a/plugins/action/k8s_info.py +++ b/plugins/action/k8s_info.py @@ -43,9 +43,11 @@ def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect new_module_args = copy.deepcopy(self._task.args) + kubeconfig = self._task.args.get('kubeconfig', None) - # find the file in the expected search path - if kubeconfig: + remote_kubeconfig = self._task.args.get('remote_kubeconfig', False) + # find the kubeconfig in the expected search path + if kubeconfig and not remote_kubeconfig: try: # find in expected paths kubeconfig = self._find_needle('files', kubeconfig) @@ -55,14 +57,20 @@ def run(self, tmp=None, task_vars=None): result['exception'] = traceback.format_exc() return result - if kubeconfig: # decrypt kubeconfig found actual_file = self._loader.get_real_file(kubeconfig, decrypt=True) new_module_args['kubeconfig'] = actual_file # find the file in the expected search path src = self._task.args.get('src', None) + remote_src = self._task.args.get('remote_src', False) + if src: + if remote_src: + # src is on remote node + result.update(self._execute_module(module_name=self._task.action, task_vars=task_vars)) + return self._ensure_invocation(result) + try: # find in expected paths src = self._find_needle('files', src) @@ -117,10 +125,6 @@ def run(self, tmp=None, task_vars=None): else: raise AnsibleActionFail("Error while reading template file - " "a string or dict for template expected, but got %s instead" % type(template)) - try: - source = self._find_needle('templates', template_path) - except AnsibleError as e: - raise AnsibleActionFail(to_text(e)) # Option `lstrip_blocks' was added in Jinja2 version 2.7. if lstrip_blocks: @@ -143,6 +147,11 @@ def run(self, tmp=None, task_vars=None): elif newline_sequence not in allowed_sequences: raise AnsibleActionFail("newline_sequence needs to be one of: \n, \r or \r\n") + try: + source = self._find_needle('templates', template_path) + except AnsibleError as e: + raise AnsibleActionFail(to_text(e)) + # Get vault decrypted tmp file try: tmp_source = self._loader.get_real_file(source) diff --git a/plugins/doc_fragments/k8s_resource_options.py b/plugins/doc_fragments/k8s_resource_options.py index b9dcfe16..35b10b84 100644 --- a/plugins/doc_fragments/k8s_resource_options.py +++ b/plugins/doc_fragments/k8s_resource_options.py @@ -30,4 +30,18 @@ class ModuleDocFragment(object): I(resource_definition). See Examples below. - Mutually exclusive with I(template) in case of M(k8s) module. type: path + remote_src: + description: + - Set to C(yes) to indicate the I(src) file is already on the remote system and not local to the Ansible controller. + - This is not valid for I(template) parameter. + type: bool + default: False + version_added: "1.2.0" + remote_kubeconfig: + description: + - Set to C(yes) to indicate the I(kubeconfig) file is already on the remote system and not local to the Ansible controller. + - Auto decryption of remote Kubeconfig file does not work. + type: bool + default: False + version_added: "1.2.0" ''' diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index 5fccdb25..5a553795 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -125,6 +125,10 @@ def list_dict_str(value): 'src': { 'type': 'path', }, + 'remote_src': { + 'type': 'bool', + 'default': False, + }, } NAME_ARG_SPEC = { @@ -141,6 +145,10 @@ def list_dict_str(value): 'kubeconfig': { 'type': 'path', }, + 'remote_kubeconfig': { + 'type': 'bool', + 'default': False, + }, 'context': {}, 'host': {}, 'api_key': {