1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Signatures

This commit is contained in:
Michael DeHaan 2014-10-02 13:24:42 -04:00
parent daab8e7ad4
commit b8e4e59d6b

View file

@ -20,59 +20,10 @@ from v2.utils import template
from v2.utils import list_union from v2.utils import list_union
class Conditional(object): class Conditional(object):
def __init__(self, basedir, conditionals=[]):
self.basedir = basedir
self.conditionals = conditionals
def push(self, conditional): def __init__(self, task):
if conditional not in self.conditionals: pass
self.conditionals.append(conditional)
def get_conditionals(self): def evaluate(self, context):
# return a full slice to make sure the reference pass
# doesn't get mangled by other users of the result
return self.conditionals[:]
def merge(self, conditional):
if isinstance(conditional, basestring):
conditional = Conditional(self.basedir, [conditional])
elif isinstance(conditional, list):
conditional = Conditional(self.basedir, conditional)
elif not isinstance(conditional, Conditional):
raise AnsibleError('expected a Conditional() class, instead got a %s' % type(conditional))
self.conditionals = list_union(self.conditionals, conditional.get_conditonals())
def evaluate(self, inject):
for conditional in self.conditional:
if not self._do_evaluate(conditional, inject):
return False
return True
def _do_evaluate(self, conditional, inject):
# allow variable names
if conditional in inject and '-' not in str(inject[conditional]):
conditional = inject[conditional]
conditional = template.template(self.basedir, conditional, inject, fail_on_undefined=C.fail_on_undefined)
original = str(conditional).replace("jinja2_compare ","")
# a Jinja2 evaluation that results in something Python can eval!
presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional
conditional = template.template(self.basedir, presented, inject)
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 "is undefined" in conditional:
return True
elif "is defined" in conditional:
return False
else:
raise errors.AnsibleError("error while evaluating conditional: %s" % original)
elif val == "True":
return True
elif val == "False":
return False
else:
raise errors.AnsibleError("unable to evaluate conditional: %s" % original)