From 294451d00243b7caf3c0aaae8476ec2a96149bdb Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Mon, 9 Sep 2013 14:50:21 -0500 Subject: [PATCH] Correctly handle variable issues when evaluating jinja2 when statements Fixes #4025 --- lib/ansible/utils/__init__.py | 15 +++++++++++++-- lib/ansible/utils/template.py | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index de6ab6b533e..b4b8b24ed34 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -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 diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 1284bc00c44..b18a9c968e6 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -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]