Correctly handle variable issues when evaluating jinja2 when statements

Fixes #4025
pull/4071/head
James Cammarata 11 years ago
parent af139cd56a
commit 294451d002

@ -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…
Cancel
Save