mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
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:
parent
49aa64a5b8
commit
501fc7a248
13 changed files with 160 additions and 2 deletions
|
@ -33,4 +33,11 @@ class AnsibleJ2Template(jinja2.environment.Template):
|
|||
'''
|
||||
|
||||
def new_context(self, vars=None, shared=False, locals=None):
|
||||
return self.environment.context_class(self.environment, vars.add_locals(locals), self.name, self.blocks)
|
||||
if vars is not None:
|
||||
if isinstance(vars, dict):
|
||||
vars = vars.copy()
|
||||
if locals is not None:
|
||||
vars.update(locals)
|
||||
else:
|
||||
vars = vars.add_locals(locals)
|
||||
return self.environment.context_class(self.environment, vars, self.name, self.blocks)
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from collections import Mapping
|
||||
|
||||
from jinja2.utils import missing
|
||||
|
||||
from ansible.module_utils.six import iteritems
|
||||
|
@ -28,7 +30,7 @@ from ansible.module_utils._text import to_native
|
|||
__all__ = ['AnsibleJ2Vars']
|
||||
|
||||
|
||||
class AnsibleJ2Vars:
|
||||
class AnsibleJ2Vars(Mapping):
|
||||
'''
|
||||
Helper class to template all variable content before jinja2 sees it. This is
|
||||
done by hijacking the variable storage that jinja2 uses, and overriding __contains__
|
||||
|
@ -69,6 +71,16 @@ class AnsibleJ2Vars:
|
|||
return True
|
||||
return False
|
||||
|
||||
def __iter__(self):
|
||||
keys = set()
|
||||
keys.update(self._templar._available_variables, self._locals, self._globals, *self._extras)
|
||||
return iter(keys)
|
||||
|
||||
def __len__(self):
|
||||
keys = set()
|
||||
keys.update(self._templar._available_variables, self._locals, self._globals, *self._extras)
|
||||
return len(keys)
|
||||
|
||||
def __getitem__(self, varname):
|
||||
if varname not in self._templar._available_variables:
|
||||
if varname in self._locals:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue