1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Properly use local variables from templates including other templates

Fixes #6653
This commit is contained in:
James Cammarata 2015-06-18 16:10:01 -04:00
parent bbcd172eb3
commit 18a9eff11f
2 changed files with 20 additions and 3 deletions

View file

@ -22,6 +22,7 @@ __metaclass__ = type
import re import re
from jinja2 import Environment from jinja2 import Environment
from jinja2.loaders import FileSystemLoader
from jinja2.exceptions import TemplateSyntaxError, UndefinedError from jinja2.exceptions import TemplateSyntaxError, UndefinedError
from jinja2.utils import concat as j2_concat from jinja2.utils import concat as j2_concat
from jinja2.runtime import StrictUndefined from jinja2.runtime import StrictUndefined
@ -71,7 +72,13 @@ class Templar:
self._fail_on_filter_errors = True self._fail_on_filter_errors = True
self._fail_on_undefined_errors = C.DEFAULT_UNDEFINED_VAR_BEHAVIOR self._fail_on_undefined_errors = C.DEFAULT_UNDEFINED_VAR_BEHAVIOR
self.environment = Environment(trim_blocks=True, undefined=StrictUndefined, extensions=self._get_extensions(), finalize=self._finalize) self.environment = Environment(
trim_blocks=True,
undefined=StrictUndefined,
extensions=self._get_extensions(),
finalize=self._finalize,
loader=FileSystemLoader('.'),
)
self.environment.template_class = AnsibleJ2Template self.environment.template_class = AnsibleJ2Template
self.SINGLE_VAR = re.compile(r"^%s\s*(\w*)\s*%s$" % (self.environment.variable_start_string, self.environment.variable_end_string)) self.SINGLE_VAR = re.compile(r"^%s\s*(\w*)\s*%s$" % (self.environment.variable_start_string, self.environment.variable_end_string))

View file

@ -19,6 +19,7 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from jinja2.utils import missing
__all__ = ['AnsibleJ2Vars'] __all__ = ['AnsibleJ2Vars']
@ -33,7 +34,7 @@ class AnsibleJ2Vars:
To facilitate using builtin jinja2 things like range, globals are also handled here. To facilitate using builtin jinja2 things like range, globals are also handled here.
''' '''
def __init__(self, templar, globals, *extras): def __init__(self, templar, globals, locals=dict(), *extras):
''' '''
Initializes this object with a valid Templar() object, as Initializes this object with a valid Templar() object, as
well as several dictionaries of variables representing well as several dictionaries of variables representing
@ -43,10 +44,17 @@ class AnsibleJ2Vars:
self._templar = templar self._templar = templar
self._globals = globals self._globals = globals
self._extras = extras self._extras = extras
self._locals = dict()
if isinstance(locals, dict):
for key, val in locals.iteritems():
if key[:2] == 'l_' and val is not missing:
self._locals[key[2:]] = val
def __contains__(self, k): def __contains__(self, k):
if k in self._templar._available_variables: if k in self._templar._available_variables:
return True return True
if k in self._locals:
return True
for i in self._extras: for i in self._extras:
if k in i: if k in i:
return True return True
@ -59,6 +67,8 @@ class AnsibleJ2Vars:
#from ansible.runner import HostVars #from ansible.runner import HostVars
if varname not in self._templar._available_variables: if varname not in self._templar._available_variables:
if varname in self._locals:
return self._locals[varname]
for i in self._extras: for i in self._extras:
if varname in i: if varname in i:
return i[varname] return i[varname]
@ -84,5 +94,5 @@ class AnsibleJ2Vars:
''' '''
if locals is None: if locals is None:
return self return self
return AnsibleJ2Vars(self._templar, self._globals, locals, *self._extras) return AnsibleJ2Vars(self._templar, self._globals, locals=locals, *self._extras)