Skip to content

Commit

Permalink
k8s: Add a parameter remote_src
Browse files Browse the repository at this point in the history
remote_src is boolean parameter to specify if
src file is located on remote node or Ansible Controller.

Fixes: ansible-collections#307

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Dec 4, 2020
1 parent 4b447cc commit 2c56113
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/307_remote_src.yml
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 16 additions & 7 deletions plugins/action/k8s_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions plugins/doc_fragments/k8s_auth_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions plugins/doc_fragments/k8s_resource_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
'''
8 changes: 8 additions & 0 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def list_dict_str(value):
'src': {
'type': 'path',
},
'remote_src': {
'type': 'bool',
'default': False,
},
}

NAME_ARG_SPEC = {
Expand All @@ -141,6 +145,10 @@ def list_dict_str(value):
'kubeconfig': {
'type': 'path',
},
'remote_kubeconfig': {
'type': 'bool',
'default': False,
},
'context': {},
'host': {},
'api_key': {
Expand Down

0 comments on commit 2c56113

Please sign in to comment.