Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template lookup: fix regression when templating hostvars #64070

Merged
merged 3 commits into from Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably need same in hostvarsvars

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is needed because deepcopy() on HostVars returns self and so deepcopy() does not continue further down the structure to be applied on HostVarsVars.


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"
Copy link
Contributor

@tremble tremble Nov 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- templated_foo == "foo\n"
- templated_foo == "{{ host_var }}\n"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the gain? In tests I don't really care about duplicate code. I prefer to use the actual values for comparisons. That way it does not depend on the templating engine (like {{hostvars['h1'].host_var}} might equal to {{ host_var }} but if it is templated to an incorrect value we won't find out because the comparison would still be true, however it's unlikely).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given it's the actual templating engine that's being tested I guess hard coded values makes sense. I would have switched to the variable just because folks looking to write their first tests often look for simple examples and assume the simple example is the way it should always be done.

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