template escaping: use configured variable start instead of default one (#81001)

this will handle escaping correctly in tempaltes that override the default jinja variable tokens
also fix optimization for single var path when overrides happen
pull/66430/merge
Brian Coca 1 year ago committed by GitHub
parent c3926268e2
commit 38067860e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- templating escape and single var optimization now use correct delimiters when custom ones are provided either via task or template header.

@ -130,7 +130,7 @@ def _escape_backslashes(data, jinja_env):
backslashes inside of a jinja2 expression.
"""
if '\\' in data and '{{' in data:
if '\\' in data and jinja_env.variable_start_string in data:
new_data = []
d2 = jinja_env.preprocess(data)
in_var = False
@ -601,11 +601,14 @@ class Templar:
# the current rendering context under which the templar class is working
self.cur_context = None
# FIXME this regex should be re-compiled each time variable_start_string and variable_end_string are changed
self.SINGLE_VAR = re.compile(r"^%s\s*(\w*)\s*%s$" % (self.environment.variable_start_string, self.environment.variable_end_string))
# this regex is re-compiled each time variable_start_string and variable_end_string are possibly changed
self._compile_single_var(self.environment)
self.jinja2_native = C.DEFAULT_JINJA2_NATIVE
def _compile_single_var(self, env):
self.SINGLE_VAR = re.compile(r"^%s\s*(\w*)\s*%s$" % (env.variable_start_string, env.variable_end_string))
def copy_with_new_env(self, environment_class=AnsibleEnvironment, **kwargs):
r"""Creates a new copy of Templar with a new environment.
@ -753,6 +756,7 @@ class Templar:
disable_lookups=disable_lookups,
convert_data=convert_data,
)
self._compile_single_var(self.environment)
return result
@ -945,8 +949,9 @@ class Templar:
try:
# NOTE Creating an overlay that lives only inside do_template means that overrides are not applied
# when templating nested variables in AnsibleJ2Vars where Templar.environment is used, not the overlay.
# This is historic behavior that is kept for backwards compatibility.
data, myenv = _create_overlay(data, overrides, self.environment)
# in case delimiters change
self._compile_single_var(myenv)
if escape_backslashes:
# Allow users to specify backslashes in playbooks as "\\" instead of as "\\\\".

Loading…
Cancel
Save