summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Krizek <martin.krizek@gmail.com>2023-01-23 16:42:38 +0100
committerGitHub <noreply@github.com>2023-01-23 09:42:38 -0600
commit83db3015e9ac7515d4a0f3a8827b46656b50990c (patch)
tree6bac3b7bea22cc8a6eb3ac8a3ca969ba77fda57f
parenta7aeb61c030b1d396c3ecd97657de2c584ee5354 (diff)
downloadansible-83db3015e9ac7515d4a0f3a8827b46656b50990c.tar.gz
Fix traceback in template action with ANSIBLE_DEBUG=1 (#79764) (#79775)
Fixes #79763 (cherry picked from commit 4f5ed249727dc0c271e07b045e514cc31e25c2de)
-rw-r--r--changelogs/fragments/79763-ansible_debug_template_tb_fix.yml2
-rw-r--r--lib/ansible/plugins/action/template.py6
-rw-r--r--test/integration/targets/var_templating/ansible_debug_template.j21
-rwxr-xr-xtest/integration/targets/var_templating/runme.sh3
-rw-r--r--test/integration/targets/var_templating/test_vars_with_sources.yml9
5 files changed, 20 insertions, 1 deletions
diff --git a/changelogs/fragments/79763-ansible_debug_template_tb_fix.yml b/changelogs/fragments/79763-ansible_debug_template_tb_fix.yml
new file mode 100644
index 0000000000..7bb7405752
--- /dev/null
+++ b/changelogs/fragments/79763-ansible_debug_template_tb_fix.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - Fix traceback when using the ``template`` module and running with ``ANSIBLE_DEBUG=1`` (https://github.com/ansible/ansible/issues/79763)
diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py
index 2e3d3641bf..d2b3df9ab7 100644
--- a/lib/ansible/plugins/action/template.py
+++ b/lib/ansible/plugins/action/template.py
@@ -118,7 +118,11 @@ class ActionModule(ActionBase):
searchpath = newsearchpath
# add ansible 'template' vars
- temp_vars = task_vars | generate_ansible_template_vars(self._task.args.get('src', None), source, dest)
+ temp_vars = task_vars.copy()
+ # NOTE in the case of ANSIBLE_DEBUG=1 task_vars is VarsWithSources(MutableMapping)
+ # so | operator cannot be used as it can be used only on dicts
+ # https://peps.python.org/pep-0584/#what-about-mapping-and-mutablemapping
+ temp_vars.update(generate_ansible_template_vars(self._task.args.get('src', None), source, dest))
# force templar to use AnsibleEnvironment to prevent issues with native types
# https://github.com/ansible/ansible/issues/46169
diff --git a/test/integration/targets/var_templating/ansible_debug_template.j2 b/test/integration/targets/var_templating/ansible_debug_template.j2
new file mode 100644
index 0000000000..8fe25f99b1
--- /dev/null
+++ b/test/integration/targets/var_templating/ansible_debug_template.j2
@@ -0,0 +1 @@
+{{ hello }}
diff --git a/test/integration/targets/var_templating/runme.sh b/test/integration/targets/var_templating/runme.sh
index 9363cb3ab2..bcf09241b0 100755
--- a/test/integration/targets/var_templating/runme.sh
+++ b/test/integration/targets/var_templating/runme.sh
@@ -16,3 +16,6 @@ ansible-playbook task_vars_templating.yml -v "$@"
# there should be an attempt to use 'sudo' in the connection debug output
ANSIBLE_BECOME_ALLOW_SAME_USER=true ansible-playbook test_connection_vars.yml -vvvv "$@" | tee /dev/stderr | grep 'sudo \-H \-S'
+
+# smoke test usage of VarsWithSources that is used when ANSIBLE_DEBUG=1
+ANSIBLE_DEBUG=1 ansible-playbook test_vars_with_sources.yml -v "$@"
diff --git a/test/integration/targets/var_templating/test_vars_with_sources.yml b/test/integration/targets/var_templating/test_vars_with_sources.yml
new file mode 100644
index 0000000000..0b8c990e60
--- /dev/null
+++ b/test/integration/targets/var_templating/test_vars_with_sources.yml
@@ -0,0 +1,9 @@
+- hosts: localhost
+ gather_facts: false
+ tasks:
+ - template:
+ src: ansible_debug_template.j2
+ dest: "{{ output_dir }}/ansible_debug_templated.txt"
+ vars:
+ output_dir: "{{ lookup('env', 'OUTPUT_DIR') }}"
+ hello: hello