From 85a82659fc93dd42e9d2d32350d687001706db50 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 3 Dec 2020 15:59:36 +0530 Subject: [PATCH 1/2] k8s: Add a parameter remote_src remote_src is boolean parameter to specify if src file is located on remote node or Ansible Controller. Fixes: #307 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/307_remote_src.yml | 3 +++ plugins/action/k8s_info.py | 23 +++++++++++++------ plugins/doc_fragments/k8s_auth_options.py | 7 ++++++ plugins/doc_fragments/k8s_resource_options.py | 7 ++++++ plugins/module_utils/common.py | 8 +++++++ 5 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/307_remote_src.yml 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_auth_options.py b/plugins/doc_fragments/k8s_auth_options.py index 053caed2..b563d839 100644 --- a/plugins/doc_fragments/k8s_auth_options.py +++ b/plugins/doc_fragments/k8s_auth_options.py @@ -28,6 +28,13 @@ class ModuleDocFragment(object): configuration file from I(~/.kube/config.json). Can also be specified via K8S_AUTH_KUBECONFIG environment variable. type: path + 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" context: description: - The name of a context found in the config file. Can also be specified via K8S_AUTH_CONTEXT environment variable. diff --git a/plugins/doc_fragments/k8s_resource_options.py b/plugins/doc_fragments/k8s_resource_options.py index b9dcfe16..a1b71c58 100644 --- a/plugins/doc_fragments/k8s_resource_options.py +++ b/plugins/doc_fragments/k8s_resource_options.py @@ -30,4 +30,11 @@ 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" ''' diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index 5ea37c46..aef2ac56 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': { From 83558756548baf5574bee302d8577a1d0a837f39 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Mon, 7 Dec 2020 17:25:11 +0530 Subject: [PATCH 2/2] remove parameters Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/307_remote_src.yml | 4 ++-- plugins/action/k8s_info.py | 14 ++++++++++---- plugins/doc_fragments/k8s_auth_options.py | 7 ------- plugins/doc_fragments/k8s_resource_options.py | 7 ------- plugins/module_utils/common.py | 8 -------- 5 files changed, 12 insertions(+), 28 deletions(-) diff --git a/changelogs/fragments/307_remote_src.yml b/changelogs/fragments/307_remote_src.yml index cbc8863e..e413da8a 100644 --- a/changelogs/fragments/307_remote_src.yml +++ b/changelogs/fragments/307_remote_src.yml @@ -1,3 +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. +- k8s - check if src file is located on remote node or on Ansible Controller (https://github.com/ansible-collections/community.kubernetes/issues/307). +- k8s - check if kubeconfig file is located on remote node or on Ansible Controller (https://github.com/ansible-collections/community.kubernetes/issues/307). diff --git a/plugins/action/k8s_info.py b/plugins/action/k8s_info.py index b28c69fa..7798a6ac 100644 --- a/plugins/action/k8s_info.py +++ b/plugins/action/k8s_info.py @@ -42,12 +42,18 @@ def run(self, tmp=None, task_vars=None): result = super(ActionModule, self).run(tmp, task_vars) del tmp # tmp no longer has any effect + # Check current transport connection and depending upon + # look for kubeconfig and src + # 'local' => look files on Ansible Controller + # Transport other than 'local' => look files on remote node + remote_transport = self._connection.transport != 'local' + new_module_args = copy.deepcopy(self._task.args) kubeconfig = self._task.args.get('kubeconfig', None) - remote_kubeconfig = self._task.args.get('remote_kubeconfig', False) # find the kubeconfig in the expected search path - if kubeconfig and not remote_kubeconfig: + if kubeconfig and not remote_transport: + # kubeconfig is local try: # find in expected paths kubeconfig = self._find_needle('files', kubeconfig) @@ -63,14 +69,14 @@ def run(self, tmp=None, task_vars=None): # 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: + if remote_transport: # src is on remote node result.update(self._execute_module(module_name=self._task.action, task_vars=task_vars)) return self._ensure_invocation(result) + # src is local try: # find in expected paths src = self._find_needle('files', src) diff --git a/plugins/doc_fragments/k8s_auth_options.py b/plugins/doc_fragments/k8s_auth_options.py index b563d839..053caed2 100644 --- a/plugins/doc_fragments/k8s_auth_options.py +++ b/plugins/doc_fragments/k8s_auth_options.py @@ -28,13 +28,6 @@ class ModuleDocFragment(object): configuration file from I(~/.kube/config.json). Can also be specified via K8S_AUTH_KUBECONFIG environment variable. type: path - 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" context: description: - The name of a context found in the config file. Can also be specified via K8S_AUTH_CONTEXT environment variable. diff --git a/plugins/doc_fragments/k8s_resource_options.py b/plugins/doc_fragments/k8s_resource_options.py index a1b71c58..b9dcfe16 100644 --- a/plugins/doc_fragments/k8s_resource_options.py +++ b/plugins/doc_fragments/k8s_resource_options.py @@ -30,11 +30,4 @@ 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" ''' diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index aef2ac56..5ea37c46 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -125,10 +125,6 @@ def list_dict_str(value): 'src': { 'type': 'path', }, - 'remote_src': { - 'type': 'bool', - 'default': False, - }, } NAME_ARG_SPEC = { @@ -145,10 +141,6 @@ def list_dict_str(value): 'kubeconfig': { 'type': 'path', }, - 'remote_kubeconfig': { - 'type': 'bool', - 'default': False, - }, 'context': {}, 'host': {}, 'api_key': {