Fix jinja2>=2.9 nested include vars (#35099)

This fixes a bug when parent's local vars where not available in nested
includes. The bug can only be seen with jinja>=2.9 which changes
how the variable scopes work.

Fixes #34886
pull/35832/head
Martin Krizek 6 years ago committed by GitHub
parent 9f9bfd819f
commit 63fdc3f08f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -121,4 +121,11 @@ class AnsibleJ2Vars(Mapping):
'''
if locals is None:
return self
return AnsibleJ2Vars(self._templar, self._globals, locals=locals, *self._extras)
# FIXME run this only on jinja2>=2.9?
# prior to version 2.9, locals contained all of the vars and not just the current
# local vars so this was not necessary for locals to propagate down to nested includes
new_locals = self._locals.copy()
new_locals.update(locals)
return AnsibleJ2Vars(self._templar, self._globals, locals=new_locals, *self._extras)

@ -32,23 +32,23 @@
template: src=foo.j2 dest={{output_dir}}/foo.templated mode=0644
register: template_result
- assert:
that:
- "'changed' in template_result"
- "'dest' in template_result"
- "'group' in template_result"
- "'gid' in template_result"
- "'md5sum' in template_result"
- assert:
that:
- "'changed' in template_result"
- "'dest' in template_result"
- "'group' in template_result"
- "'gid' in template_result"
- "'md5sum' in template_result"
- "'checksum' in template_result"
- "'owner' in template_result"
- "'size' in template_result"
- "'src' in template_result"
- "'state' in template_result"
- "'uid' in template_result"
- "'owner' in template_result"
- "'size' in template_result"
- "'src' in template_result"
- "'state' in template_result"
- "'uid' in template_result"
- name: verify that the file was marked as changed
assert:
that:
assert:
that:
- "template_result.changed == true"
# test for import with context on jinja-2.9 See https://github.com/ansible/ansible/issues/20494
@ -69,6 +69,21 @@
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"
# test for nested include https://github.com/ansible/ansible/issues/34886
- name: test if parent variables are defined in nested include
template: src=for_loop.j2 dest={{output_dir}}/for_loop.templated mode=0644
- name: save templated output
shell: "cat {{output_dir}}/for_loop.templated"
register: for_loop_out
- debug: var=for_loop_out
- name: verify variables got templated
assert:
that:
- '"foo" in for_loop_out.stdout'
- '"bar" in for_loop_out.stdout'
- '"bam" in for_loop_out.stdout'
# test for 'import as' on jinja-2.9 See https://github.com/ansible/ansible/issues/20494
- name: fill in a template using import as ala fails2 case in issue 20494
template: src=import_as.j2 dest={{output_dir}}/import_as.templated mode=0644
@ -258,8 +273,8 @@
register: templated
- name: verify that the file was marked as changed in check mode but was not created
assert:
that:
assert:
that:
- "not templated.stat.exists"
- "template_result is changed"
@ -272,13 +287,13 @@
check_mode: True
- name: verify that the file was marked as not changes in check mode
assert:
that:
assert:
that:
- "template_result is not changed"
- "'templated_var_loaded' in lookup('file', '{{output_dir | expanduser}}/short.templated')"
- name: change var for the template
set_fact:
set_fact:
templated_var: "changed"
- name: fill in a basic template with changed var in check mode
@ -287,8 +302,8 @@
check_mode: True
- name: verify that the file was marked as changed in check mode but the content was not changed
assert:
that:
assert:
that:
- "'templated_var_loaded' in lookup('file', '{{output_dir | expanduser }}/short.templated')"
- "template_result is changed"

@ -0,0 +1,4 @@
{% for par_var in parent_vars %}
{% include 'for_loop_include.j2' %}
{% endfor %}

@ -0,0 +1,3 @@
{% if par_var is defined %}
{% include 'for_loop_include_nested.j2' %}
{% endif %}
Loading…
Cancel
Save