mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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
This commit is contained in:
parent
9f9bfd819f
commit
63fdc3f08f
5 changed files with 52 additions and 22 deletions
|
@ -121,4 +121,11 @@ class AnsibleJ2Vars(Mapping):
|
||||||
'''
|
'''
|
||||||
if locals is None:
|
if locals is None:
|
||||||
return self
|
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)
|
||||||
|
|
|
@ -69,6 +69,21 @@
|
||||||
- 'diff_result.stdout == ""'
|
- 'diff_result.stdout == ""'
|
||||||
- "diff_result.rc == 0"
|
- "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
|
# 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
|
- 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
|
template: src=import_as.j2 dest={{output_dir}}/import_as.templated mode=0644
|
||||||
|
|
4
test/integration/targets/template/templates/for_loop.j2
Normal file
4
test/integration/targets/template/templates/for_loop.j2
Normal file
|
@ -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 %}
|
|
@ -0,0 +1 @@
|
||||||
|
{{ par_var }}
|
Loading…
Reference in a new issue