diff --git a/changelogs/fragments/63940-template-lookup-hostvars-regression.yml b/changelogs/fragments/63940-template-lookup-hostvars-regression.yml new file mode 100644 index 00000000000..2eefdcf01e9 --- /dev/null +++ b/changelogs/fragments/63940-template-lookup-hostvars-regression.yml @@ -0,0 +1,2 @@ +bugfixes: + - template lookup - fix regression when templating hostvars (https://github.com/ansible/ansible/issues/63940) diff --git a/lib/ansible/vars/hostvars.py b/lib/ansible/vars/hostvars.py index db0e0591065..1d3c5ef9432 100644 --- a/lib/ansible/vars/hostvars.py +++ b/lib/ansible/vars/hostvars.py @@ -111,6 +111,12 @@ class HostVars(Mapping): out[host] = self.get(host) return repr(out) + def __deepcopy__(self, memo): + # We do not need to deepcopy because HostVars is immutable, + # however we have to implement the method so we can deepcopy + # variables' dicts that contain HostVars. + return self + class HostVarsVars(Mapping): diff --git a/test/integration/targets/lookups/runme.sh b/test/integration/targets/lookups/runme.sh index e92afab462d..698ff190456 100755 --- a/test/integration/targets/lookups/runme.sh +++ b/test/integration/targets/lookups/runme.sh @@ -11,3 +11,5 @@ pip install passlib ANSIBLE_ROLES_PATH=../ ansible-playbook lookups.yml "$@" ansible-playbook template_lookup_vaulted.yml --vault-password-file test_vault_pass "$@" + +ansible-playbook -i template_deepcopy/hosts template_deepcopy/playbook.yml "$@" diff --git a/test/integration/targets/lookups/template_deepcopy/hosts b/test/integration/targets/lookups/template_deepcopy/hosts new file mode 100644 index 00000000000..ecd3b9665b3 --- /dev/null +++ b/test/integration/targets/lookups/template_deepcopy/hosts @@ -0,0 +1 @@ +h1 ansible_connection=local host_var=foo diff --git a/test/integration/targets/lookups/template_deepcopy/playbook.yml b/test/integration/targets/lookups/template_deepcopy/playbook.yml new file mode 100644 index 00000000000..da55c16752b --- /dev/null +++ b/test/integration/targets/lookups/template_deepcopy/playbook.yml @@ -0,0 +1,10 @@ +- hosts: h1 + gather_facts: no + tasks: + - set_fact: + templated_foo: "{{ lookup('template', 'template.in') }}" + + - name: Test that the hostvar was templated correctly + assert: + that: + - templated_foo == "foo\n" diff --git a/test/integration/targets/lookups/template_deepcopy/template.in b/test/integration/targets/lookups/template_deepcopy/template.in new file mode 100644 index 00000000000..77de0adf827 --- /dev/null +++ b/test/integration/targets/lookups/template_deepcopy/template.in @@ -0,0 +1 @@ +{{hostvars['h1'].host_var}}