|
|
@ -22,7 +22,7 @@ __metaclass__ = type
|
|
|
|
from jinja2.exceptions import UndefinedError
|
|
|
|
from jinja2.exceptions import UndefinedError
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.compat.six import text_type
|
|
|
|
from ansible.compat.six import text_type
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.errors import AnsibleError, AnsibleUndefinedVariable
|
|
|
|
from ansible.playbook.attribute import FieldAttribute
|
|
|
|
from ansible.playbook.attribute import FieldAttribute
|
|
|
|
from ansible.template import Templar
|
|
|
|
from ansible.template import Templar
|
|
|
|
|
|
|
|
|
|
|
@ -89,16 +89,22 @@ class Conditional:
|
|
|
|
# make sure the templar is using the variables specifed to this method
|
|
|
|
# make sure the templar is using the variables specifed to this method
|
|
|
|
templar.set_available_variables(variables=all_vars)
|
|
|
|
templar.set_available_variables(variables=all_vars)
|
|
|
|
|
|
|
|
|
|
|
|
conditional = templar.template(conditional)
|
|
|
|
try:
|
|
|
|
if not isinstance(conditional, basestring) or conditional == "":
|
|
|
|
conditional = templar.template(conditional)
|
|
|
|
return conditional
|
|
|
|
if not isinstance(conditional, text_type) or conditional == "":
|
|
|
|
|
|
|
|
return conditional
|
|
|
|
# a Jinja2 evaluation that results in something Python can eval!
|
|
|
|
|
|
|
|
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
|
|
|
|
# a Jinja2 evaluation that results in something Python can eval!
|
|
|
|
conditional = templar.template(presented, fail_on_undefined=False)
|
|
|
|
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
|
|
|
|
|
|
|
|
conditional = templar.template(presented)
|
|
|
|
val = conditional.strip()
|
|
|
|
val = conditional.strip()
|
|
|
|
if val == presented:
|
|
|
|
if val == "True":
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
elif val == "False":
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise AnsibleError("unable to evaluate conditional: %s" % original)
|
|
|
|
|
|
|
|
except (AnsibleUndefinedVariable, UndefinedError) as e:
|
|
|
|
# the templating failed, meaning most likely a
|
|
|
|
# the templating failed, meaning most likely a
|
|
|
|
# variable was undefined. If we happened to be
|
|
|
|
# variable was undefined. If we happened to be
|
|
|
|
# looking for an undefined variable, return True,
|
|
|
|
# looking for an undefined variable, return True,
|
|
|
@ -108,11 +114,5 @@ class Conditional:
|
|
|
|
elif "is defined" in original:
|
|
|
|
elif "is defined" in original:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise AnsibleError("error while evaluating conditional: %s (%s)" % (original, presented))
|
|
|
|
raise AnsibleError("error while evaluating conditional (%s): %s" % (original, e))
|
|
|
|
elif val == "True":
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
elif val == "False":
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise AnsibleError("unable to evaluate conditional: %s" % original)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|