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

add when_failed, when_changed, and extended when_set/unset

This commit extends the 'when_' conditions to failed and changed
json results

Additionally it makes when_{set,unset,failed,changed,int,str,flt}
behave more similiarily in that they all except and/or/not logic
This commit is contained in:
fdavis 2012-12-31 17:27:16 -08:00
parent 1b5d039bf4
commit fe310dcff1

View file

@ -135,6 +135,11 @@ def is_failed(result):
return ((result.get('rc', 0) != 0) or (result.get('failed', False) in [ True, 'True', 'true'])) return ((result.get('rc', 0) != 0) or (result.get('failed', False) in [ True, 'True', 'true']))
def is_changed(result):
''' is a given JSON result a changed result? '''
return (result.get('changed', False) in [ True, 'True', 'true'])
def check_conditional(conditional): def check_conditional(conditional):
def is_set(var): def is_set(var):
@ -484,6 +489,8 @@ def compile_when_to_only_if(expression):
# when: set $variable # when: set $variable
# when: unset $variable # when: unset $variable
# when: failed $json_result
# when: changed $json_result
# when: int $x >= $z and $y < 3 # when: int $x >= $z and $y < 3
# when: int $x in $alist # when: int $x in $alist
# when: float $x > 2 and $y <= $z # when: float $x > 2 and $y <= $z
@ -497,9 +504,27 @@ def compile_when_to_only_if(expression):
# when_set / when_unset # when_set / when_unset
if tokens[0] in [ 'set', 'unset' ]: if tokens[0] in [ 'set', 'unset' ]:
if len(tokens) != 2: tcopy = tokens[1:]
raise errors.AnsibleError("usage: when: <set|unset> <$variableName>") for (i,t) in enumerate(tokens[1:]):
return "is_%s('''%s''')" % (tokens[0], tokens[1]) if t.find("$") != -1:
tcopy[i] = "is_%s('''%s''')" % (tokens[0], t)
else:
tcopy[i] = t
return " ".join(tcopy)
# when_failed / when_changed
elif tokens[0] in [ 'failed', 'changed' ]:
tcopy = tokens[1:]
for (i,t) in enumerate(tokens[1:]):
if t.find("$") != -1:
tcopy[i] = "is_%s(%s)" % (tokens[0], t)
else:
tcopy[i] = t
return " ".join(tcopy)
# when_integer / when_float / when_string # when_integer / when_float / when_string
elif tokens[0] in [ 'integer', 'float', 'string' ]: elif tokens[0] in [ 'integer', 'float', 'string' ]: