mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Do not escape backslashes when processing a template file.
Fixes #12198 Fixes #12241
This commit is contained in:
parent
21ee4b02da
commit
e218bf8da9
4 changed files with 32 additions and 15 deletions
|
@ -122,7 +122,7 @@ class ActionModule(ActionBase):
|
|||
|
||||
old_vars = self._templar._available_variables
|
||||
self._templar.set_available_variables(temp_vars)
|
||||
resultant = self._templar.template(template_data, preserve_trailing_newlines=True, convert_data=False)
|
||||
resultant = self._templar.template(template_data, preserve_trailing_newlines=True, escape_backslashes=False, convert_data=False)
|
||||
self._templar.set_available_variables(old_vars)
|
||||
except Exception as e:
|
||||
return dict(failed=True, msg=type(e).__name__ + ": " + str(e))
|
||||
|
|
|
@ -48,7 +48,7 @@ NON_TEMPLATED_TYPES = ( bool, Number )
|
|||
|
||||
JINJA2_OVERRIDE = '#jinja2:'
|
||||
|
||||
def _preserve_backslashes(data, jinja_env):
|
||||
def _escape_backslashes(data, jinja_env):
|
||||
"""Double backslashes within jinja2 expressions
|
||||
|
||||
A user may enter something like this in a playbook::
|
||||
|
@ -206,7 +206,7 @@ class Templar:
|
|||
assert isinstance(variables, dict)
|
||||
self._available_variables = variables.copy()
|
||||
|
||||
def template(self, variable, convert_bare=False, preserve_trailing_newlines=True, fail_on_undefined=None, overrides=None, convert_data=True):
|
||||
def template(self, variable, convert_bare=False, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None, convert_data=True):
|
||||
'''
|
||||
Templates (possibly recursively) any given data as input. If convert_bare is
|
||||
set to True, the given data will be wrapped as a jinja2 variable ('{{foo}}')
|
||||
|
@ -234,7 +234,7 @@ class Templar:
|
|||
elif resolved_val is None:
|
||||
return C.DEFAULT_NULL_REPRESENTATION
|
||||
|
||||
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
|
||||
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, escape_backslashes=escape_backslashes, fail_on_undefined=fail_on_undefined, overrides=overrides)
|
||||
|
||||
if convert_data:
|
||||
# if this looks like a dictionary or list, convert it to such using the safe_eval method
|
||||
|
@ -316,7 +316,7 @@ class Templar:
|
|||
else:
|
||||
raise AnsibleError("lookup plugin (%s) not found" % name)
|
||||
|
||||
def _do_template(self, data, preserve_trailing_newlines=True, fail_on_undefined=None, overrides=None):
|
||||
def _do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None):
|
||||
|
||||
# For preserving the number of input newlines in the output (used
|
||||
# later in this method)
|
||||
|
@ -346,7 +346,10 @@ class Templar:
|
|||
myenv.filters.update(self._get_filters())
|
||||
myenv.tests.update(self._get_tests())
|
||||
|
||||
data = _preserve_backslashes(data, myenv)
|
||||
if escape_backslashes:
|
||||
# Allow users to specify backslashes in playbooks as "\\"
|
||||
# instead of as "\\\\".
|
||||
data = _escape_backslashes(data, myenv)
|
||||
|
||||
try:
|
||||
t = myenv.from_string(data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue