mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Correctly handle variable issues when evaluating jinja2 when statements
Fixes #4025
This commit is contained in:
parent
af139cd56a
commit
294451d002
2 changed files with 14 additions and 3 deletions
|
@ -170,8 +170,19 @@ def check_conditional(conditional, basedir, inject, fail_on_undefined=False, jin
|
|||
# a Jinja2 evaluation that results in something Python can eval!
|
||||
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
|
||||
conditional = template.template(basedir, presented, inject)
|
||||
val = conditional.lstrip().rstrip()
|
||||
if val == "True":
|
||||
val = conditional.strip()
|
||||
if val == presented:
|
||||
# the templating failed, meaning most likely a
|
||||
# variable was undefined. If we happened to be
|
||||
# looking for an undefined variable, return True,
|
||||
# otherwise fail
|
||||
if conditional.find("is undefined") != -1:
|
||||
return True
|
||||
elif conditional.find("is defined") != -1:
|
||||
return False
|
||||
else:
|
||||
raise errors.AnsibleError("error while evaluating conditional: %s" % conditional)
|
||||
elif val == "True":
|
||||
return True
|
||||
elif val == "False":
|
||||
return False
|
||||
|
|
|
@ -430,7 +430,7 @@ def template_from_file(basedir, path, vars):
|
|||
raise errors.AnsibleError("unable to read %s" % realpath)
|
||||
|
||||
|
||||
# Get jinja env overrides from template
|
||||
# Get jinja env overrides from template
|
||||
if data.startswith(JINJA2_OVERRIDE):
|
||||
eol = data.find('\n')
|
||||
line = data[len(JINJA2_OVERRIDE):eol]
|
||||
|
|
Loading…
Reference in a new issue