template lookup: fix regression when templating hostvars (#64070)

This fixes a regression that was caused by switching from copy() to
deepcopy() when 'saving' variables before templating. Since HostVars
did not implement the __deepcopy__() method, deepcopy returned incorrect
results when host vars were present in the variables.

Fixes #63940
This commit is contained in:
Martin Krizek 2019-11-06 15:25:43 +01:00 committed by GitHub
parent 22fe622589
commit cd8ce16d48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- template lookup - fix regression when templating hostvars (https://github.com/ansible/ansible/issues/63940)

View file

@ -111,6 +111,12 @@ class HostVars(Mapping):
out[host] = self.get(host) out[host] = self.get(host)
return repr(out) 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): class HostVarsVars(Mapping):

View file

@ -11,3 +11,5 @@ pip install passlib
ANSIBLE_ROLES_PATH=../ ansible-playbook lookups.yml "$@" ANSIBLE_ROLES_PATH=../ ansible-playbook lookups.yml "$@"
ansible-playbook template_lookup_vaulted.yml --vault-password-file test_vault_pass "$@" ansible-playbook template_lookup_vaulted.yml --vault-password-file test_vault_pass "$@"
ansible-playbook -i template_deepcopy/hosts template_deepcopy/playbook.yml "$@"

View file

@ -0,0 +1 @@
h1 ansible_connection=local host_var=foo

View file

@ -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"

View file

@ -0,0 +1 @@
{{hostvars['h1'].host_var}}