Skip to content

Commit

Permalink
template lookup: fix regression when templating hostvars (ansible#64070)
Browse files Browse the repository at this point in the history
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 ansible#63940
  • Loading branch information
mkrizek committed Nov 6, 2019
1 parent 22fe622 commit cd8ce16
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
@@ -0,0 +1,2 @@
bugfixes:
- template lookup - fix regression when templating hostvars (https://github.com/ansible/ansible/issues/63940)
6 changes: 6 additions & 0 deletions lib/ansible/vars/hostvars.py
Expand Up @@ -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):

Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/lookups/runme.sh
Expand Up @@ -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 "$@"
1 change: 1 addition & 0 deletions test/integration/targets/lookups/template_deepcopy/hosts
@@ -0,0 +1 @@
h1 ansible_connection=local host_var=foo
10 changes: 10 additions & 0 deletions 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"
@@ -0,0 +1 @@
{{hostvars['h1'].host_var}}

0 comments on commit cd8ce16

Please sign in to comment.