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! # a Jinja2 evaluation that results in something Python can eval!
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
conditional = template.template(basedir, presented, inject) conditional = template.template(basedir, presented, inject)
val = conditional.lstrip().rstrip() val = conditional.strip()
if val == "True": 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 return True
elif val == "False": elif val == "False":
return False return False

@ -430,7 +430,7 @@ def template_from_file(basedir, path, vars):
raise errors.AnsibleError("unable to read %s" % realpath) 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): if data.startswith(JINJA2_OVERRIDE):
eol = data.find('\n') eol = data.find('\n')
line = data[len(JINJA2_OVERRIDE):eol] line = data[len(JINJA2_OVERRIDE):eol]

Loading…
Cancel
Save