mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Allow only_if/when_* on task includes and apply it on all tasks within
This commit is contained in:
@@ -476,4 +476,57 @@ def boolean(value):
|
||||
else:
|
||||
return False
|
||||
|
||||
def compile_when_to_only_if(expression):
|
||||
'''
|
||||
when is a shorthand for writing only_if conditionals. It requires less quoting
|
||||
magic. only_if is retained for backwards compatibility.
|
||||
'''
|
||||
|
||||
# when: set $variable
|
||||
# when: unset $variable
|
||||
# when: int $x >= $z and $y < 3
|
||||
# when: int $x in $alist
|
||||
# when: float $x > 2 and $y <= $z
|
||||
# when: str $x != $y
|
||||
|
||||
if type(expression) not in [ str, unicode ]:
|
||||
raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression)
|
||||
tokens = expression.split()
|
||||
if len(tokens) < 2:
|
||||
raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression)
|
||||
|
||||
# when_set / when_unset
|
||||
if tokens[0] in [ 'set', 'unset' ]:
|
||||
if len(tokens) != 2:
|
||||
raise errors.AnsibleError("usage: when: <set|unset> <$variableName>")
|
||||
return "is_%s('''%s''')" % (tokens[0], tokens[1])
|
||||
|
||||
# when_integer / when_float / when_string
|
||||
elif tokens[0] in [ 'integer', 'float', 'string' ]:
|
||||
cast = None
|
||||
if tokens[0] == 'integer':
|
||||
cast = 'int'
|
||||
elif tokens[0] == 'string':
|
||||
cast = 'str'
|
||||
elif tokens[0] == 'float':
|
||||
cast = 'float'
|
||||
tcopy = tokens[1:]
|
||||
for (i,t) in enumerate(tokens[1:]):
|
||||
if t.find("$") != -1:
|
||||
# final variable substitution will happen in Runner code
|
||||
tcopy[i] = "%s('''%s''')" % (cast, t)
|
||||
else:
|
||||
tcopy[i] = t
|
||||
return " ".join(tcopy)
|
||||
|
||||
# when_boolean
|
||||
elif tokens[0] in [ 'bool', 'boolean' ]:
|
||||
tcopy = tokens[1:]
|
||||
for (i, t) in enumerate(tcopy):
|
||||
if t.find("$") != -1:
|
||||
tcopy[i] = "(is_set('''%s''') and '''%s'''.lower() not in ('false', 'no', 'n', 'none', '0', ''))" % (t, t)
|
||||
return " ".join(tcopy)
|
||||
|
||||
else:
|
||||
raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user