Templar: encapsulate _available_variables (#55435)

Ensure variables are reset between iterations
This commit is contained in:
Martin Krizek
2019-05-20 17:49:54 +02:00
committed by Brian Coca
parent bd061fd632
commit 34e9d6781b
14 changed files with 47 additions and 30 deletions

View File

@@ -144,10 +144,10 @@ class ActionModule(ActionBase):
temp_vars = task_vars.copy()
temp_vars.update(generate_ansible_template_vars(source, dest))
old_vars = self._templar._available_variables
self._templar.set_available_variables(temp_vars)
old_vars = self._templar.available_variables
self._templar.available_variables = temp_vars
resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
self._templar.set_available_variables(old_vars)
self._templar.available_variables = old_vars
except AnsibleAction:
raise
except Exception as e:

View File

@@ -349,7 +349,7 @@ class Constructable(object):
def _compose(self, template, variables):
''' helper method for plugins to compose variables for Ansible based on jinja2 expression and inventory vars'''
t = self.templar
t.set_available_variables(variables)
t.available_variables = variables
return t.template('%s%s%s' % (t.environment.variable_start_string, template, t.environment.variable_end_string), disable_lookups=True)
def _set_composite_vars(self, compose, variables, host, strict=False):
@@ -369,7 +369,7 @@ class Constructable(object):
# process each 'group entry'
if groups and isinstance(groups, dict):
variables = combine_vars(variables, self.inventory.get_host(host).get_vars())
self.templar.set_available_variables(variables)
self.templar.available_variables = variables
for group_name in groups:
conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % groups[group_name]
group_name = self._sanitize_group_name(group_name)

View File

@@ -348,7 +348,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
# FUTURE: fix underlying inventory stuff to allow us to quickly access known groupvars from reconciled host
def _filter_host(self, inventory_hostname, hostvars):
self.templar.set_available_variables(hostvars)
self.templar.available_variables = hostvars
for condition in self._filters:
# FUTURE: should warn/fail if conditional doesn't return True or False

View File

@@ -101,9 +101,8 @@ class InventoryModule(BaseInventoryPlugin):
return valid
def template(self, pattern, variables):
t = self.templar
t.set_available_variables(variables)
return t.do_template(pattern)
self.templar.available_variables = variables
return self.templar.do_template(pattern)
def add_parents(self, inventory, child, parents, template_vars):
for parent in parents:

View File

@@ -68,7 +68,7 @@ class LookupModule(LookupBase):
variable_start_string = kwargs.get('variable_start_string', None)
variable_end_string = kwargs.get('variable_end_string', None)
old_vars = self._templar._available_variables
old_vars = self._templar.available_variables
for term in terms:
display.debug("File lookup term: %s" % term)
@@ -105,7 +105,7 @@ class LookupModule(LookupBase):
vars = deepcopy(variables)
vars.update(generate_ansible_template_vars(lookupfile))
vars.update(lookup_template_vars)
self._templar.set_available_variables(vars)
self._templar.available_variables = vars
# do the templating
res = self._templar.template(template_data, preserve_trailing_newlines=True,
@@ -116,6 +116,6 @@ class LookupModule(LookupBase):
raise AnsibleError("the template file %s could not be found for the lookup" % term)
# restore old variables
self._templar.set_available_variables(old_vars)
self._templar.available_variables = old_vars
return ret

View File

@@ -66,7 +66,7 @@ class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
if variables is not None:
self._templar.set_available_variables(variables)
self._templar.available_variables = variables
myvars = getattr(self._templar, '_available_variables', {})
self.set_options(direct=kwargs)