template: fix KeyError: 'undefined variable: 0 (#27972)

* template: fix KeyError: 'undefined variable: 0

For compatibility with the Context.get_all() implementation
in jinja 2.9, make AnsibleJ2Vars implement collections.Mapping.
Also, make AnsibleJ2Template.newcontext() handle dict type
for the 'vars' parameter.

See: d67f0fd4cc
Fixes: https://github.com/ansible/ansible/issues/20494

* add units/template/test_vars

* intg tests for jinja-2.9 issues like 20494

test cases here are based on
https://github.com/ansible/ansible/issues/20494#issue-202108318
This commit is contained in:
Zac Medico 2017-08-09 15:50:53 -07:00 committed by Brian Coca
commit 501fc7a248
13 changed files with 160 additions and 2 deletions

View file

@ -0,0 +1,3 @@
hello world import as
WIBBLE
Goodbye

View file

@ -0,0 +1,2 @@
hello world as qux with context
WIBBLE

View file

@ -0,0 +1,3 @@
hello world with context
WIBBLE
Goodbye

View file

@ -51,6 +51,60 @@
that:
- "template_result.changed == true"
# test for import with context on jinja-2.9 See https://github.com/ansible/ansible/issues/20494
- name: fill in a template using import with context ala issue 20494
template: src=import_with_context.j2 dest={{output_dir}}/import_with_context.templated mode=0644
register: template_result
- name: copy known good import_with_context.expected into place
copy: src=import_with_context.expected dest={{output_dir}}/import_with_context.expected
- name: compare templated file to known good import_with_context
shell: diff -uw {{output_dir}}/import_with_context.templated {{output_dir}}/import_with_context.expected
register: diff_result
- name: verify templated import_with_context matches known good
assert:
that:
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"
# 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
register: import_as_template_result
- name: copy known good import_as.expected into place
copy: src=import_as.expected dest={{output_dir}}/import_as.expected
- name: compare templated file to known good import_as
shell: diff -uw {{output_dir}}/import_as.templated {{output_dir}}/import_as.expected
register: import_as_diff_result
- name: verify templated import_as matches known good
assert:
that:
- 'import_as_diff_result.stdout == ""'
- "import_as_diff_result.rc == 0"
# test for 'import as with context' on jinja-2.9 See https://github.com/ansible/ansible/issues/20494
- name: fill in a template using import as with context ala fails2 case in issue 20494
template: src=import_as_with_context.j2 dest={{output_dir}}/import_as_with_context.templated mode=0644
register: import_as_with_context_template_result
- name: copy known good import_as_with_context.expected into place
copy: src=import_as_with_context.expected dest={{output_dir}}/import_as_with_context.expected
- name: compare templated file to known good import_as_with_context
shell: diff -uw {{output_dir}}/import_as_with_context.templated {{output_dir}}/import_as_with_context.expected
register: import_as_with_context_diff_result
- name: verify templated import_as_with_context matches known good
assert:
that:
- 'import_as_with_context_diff_result.stdout == ""'
- "import_as_with_context_diff_result.rc == 0"
# VERIFY CONTENTS
- name: check what python version ansible is running on

View file

@ -0,0 +1 @@
Goodbye

View file

@ -0,0 +1,4 @@
{% import 'qux' as qux %}
hello world import as
{{ qux.wibble }}
{% include 'bar' %}

View file

@ -0,0 +1,3 @@
{% import 'qux' as qux with context %}
hello world as qux with context
{{ qux.wibble }}

View file

@ -0,0 +1,3 @@
{% import 'qux' as qux with context %}
hello world as qux with context
{{ qux.wibble }}

View file

@ -0,0 +1,4 @@
{% import 'qux' as qux with context %}
hello world with context
{{ qux.wibble }}
{% include 'bar' %}

View file

@ -0,0 +1 @@
{% set wibble = "WIBBLE" %}