k8s: handle remote src and kubeconfig (#320)

* 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 <akasurde@redhat.com>

* remove parameters

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde
2020-12-10 23:28:33 +05:30
committed by GitHub
parent befc0f6853
commit 5d3e465672
2 changed files with 25 additions and 7 deletions

View File

@@ -0,0 +1,3 @@
minor_changes:
- 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).

View File

@@ -42,10 +42,18 @@ class ActionModule(ActionBase):
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)
# find the file in the expected search path
if kubeconfig:
# find the kubeconfig in the expected search path
if kubeconfig and not remote_transport:
# kubeconfig is local
try:
# find in expected paths
kubeconfig = self._find_needle('files', kubeconfig)
@@ -55,14 +63,20 @@ class ActionModule(ActionBase):
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)
if 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)
@@ -117,10 +131,6 @@ class ActionModule(ActionBase):
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 +153,11 @@ class ActionModule(ActionBase):
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)