mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-05 16:51:30 -07:00
template lookup: restore variables between calls (#55126)
* template lookup: restore variables between calls Fixes #55113 * Address issues from the review
This commit is contained in:
parent
f6fbfeace8
commit
f37476e247
2 changed files with 10 additions and 1 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- template lookup - restore variables between calls (https://github.com/ansible/ansible/issues/55113)
|
|
@ -45,6 +45,7 @@ _raw:
|
||||||
description: file(s) content after templating
|
description: file(s) content after templating
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from ansible.errors import AnsibleError
|
from ansible.errors import AnsibleError
|
||||||
|
@ -67,6 +68,8 @@ class LookupModule(LookupBase):
|
||||||
variable_start_string = kwargs.get('variable_start_string', None)
|
variable_start_string = kwargs.get('variable_start_string', None)
|
||||||
variable_end_string = kwargs.get('variable_end_string', None)
|
variable_end_string = kwargs.get('variable_end_string', None)
|
||||||
|
|
||||||
|
old_vars = self._templar._available_variables
|
||||||
|
|
||||||
for term in terms:
|
for term in terms:
|
||||||
display.debug("File lookup term: %s" % term)
|
display.debug("File lookup term: %s" % term)
|
||||||
|
|
||||||
|
@ -99,7 +102,7 @@ class LookupModule(LookupBase):
|
||||||
# plus some added by ansible (e.g., template_{path,mtime}),
|
# plus some added by ansible (e.g., template_{path,mtime}),
|
||||||
# plus anything passed to the lookup with the template_vars=
|
# plus anything passed to the lookup with the template_vars=
|
||||||
# argument.
|
# argument.
|
||||||
vars = variables.copy()
|
vars = deepcopy(variables)
|
||||||
vars.update(generate_ansible_template_vars(lookupfile))
|
vars.update(generate_ansible_template_vars(lookupfile))
|
||||||
vars.update(lookup_template_vars)
|
vars.update(lookup_template_vars)
|
||||||
self._templar.set_available_variables(vars)
|
self._templar.set_available_variables(vars)
|
||||||
|
@ -107,8 +110,12 @@ class LookupModule(LookupBase):
|
||||||
# do the templating
|
# do the templating
|
||||||
res = self._templar.template(template_data, preserve_trailing_newlines=True,
|
res = self._templar.template(template_data, preserve_trailing_newlines=True,
|
||||||
convert_data=convert_data_p, escape_backslashes=False)
|
convert_data=convert_data_p, escape_backslashes=False)
|
||||||
|
|
||||||
ret.append(res)
|
ret.append(res)
|
||||||
else:
|
else:
|
||||||
raise AnsibleError("the template file %s could not be found for the lookup" % term)
|
raise AnsibleError("the template file %s could not be found for the lookup" % term)
|
||||||
|
|
||||||
|
# restore old variables
|
||||||
|
self._templar.set_available_variables(old_vars)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue