From 83db3015e9ac7515d4a0f3a8827b46656b50990c Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Mon, 23 Jan 2023 16:42:38 +0100 Subject: [PATCH] Fix traceback in template action with ANSIBLE_DEBUG=1 (#79764) (#79775) Fixes #79763 (cherry picked from commit 4f5ed249727dc0c271e07b045e514cc31e25c2de) --- .../fragments/79763-ansible_debug_template_tb_fix.yml | 2 ++ lib/ansible/plugins/action/template.py | 6 +++++- .../targets/var_templating/ansible_debug_template.j2 | 1 + test/integration/targets/var_templating/runme.sh | 3 +++ .../targets/var_templating/test_vars_with_sources.yml | 9 +++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/79763-ansible_debug_template_tb_fix.yml create mode 100644 test/integration/targets/var_templating/ansible_debug_template.j2 create mode 100644 test/integration/targets/var_templating/test_vars_with_sources.yml 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 00000000000..7bb7405752d --- /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 2e3d3641bf4..d2b3df9ab7b 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 00000000000..8fe25f99b1c --- /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 9363cb3ab29..bcf09241b07 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 00000000000..0b8c990e602 --- /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