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 00000000000000..2eefdcf01e9d7f --- /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 db0e0591065b32..1d3c5ef94320cd 100644 --- a/lib/ansible/vars/hostvars.py +++ b/lib/ansible/vars/hostvars.py @@ -111,6 +111,12 @@ def __repr__(self): 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 e92afab462d7d5..698ff190456663 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 00000000000000..ecd3b9665b3284 --- /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 00000000000000..da55c16752bdc3 --- /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 00000000000000..77de0adf82736c --- /dev/null +++ b/test/integration/targets/lookups/template_deepcopy/template.in @@ -0,0 +1 @@ +{{hostvars['h1'].host_var}}