2012-03-20 23:55:24 +00:00
|
|
|
---
|
2012-10-27 21:55:35 +00:00
|
|
|
# this is a demo of conditional executions using 'when' statements, which can skip
|
|
|
|
# certain tasks on machines/platforms/etc where they do not apply.
|
2012-03-20 23:55:24 +00:00
|
|
|
|
|
|
|
- hosts: all
|
|
|
|
user: root
|
|
|
|
|
|
|
|
vars:
|
|
|
|
favcolor: "red"
|
2012-10-27 21:55:35 +00:00
|
|
|
dog: "fido"
|
|
|
|
cat: "whiskers"
|
2012-03-20 23:55:24 +00:00
|
|
|
ssn: 8675309
|
|
|
|
|
2013-05-01 21:11:19 +00:00
|
|
|
# These are the types of when statements available
|
2012-10-27 21:55:35 +00:00
|
|
|
# when_set: $variable_name
|
|
|
|
# when_unset: $variable_name
|
|
|
|
# when_str: $x == "test"
|
|
|
|
# when_int: $y > 2
|
|
|
|
# when_float: $z => 2.3
|
2012-03-22 00:00:48 +00:00
|
|
|
#
|
2012-10-27 21:55:35 +00:00
|
|
|
# when using 'when', take care to make sure any variables given are surrounded by spaces
|
|
|
|
# as an example, $z>3 will not do what you want, use "$z > 3"
|
2012-10-27 22:22:16 +00:00
|
|
|
#
|
|
|
|
# note, if you are doing comparisons to variables that are stored as hashes or lists,
|
|
|
|
# you will need to use the older 'only_if', which is more free form.
|
|
|
|
# see conditionals_part_3.yml
|
2012-03-20 23:55:24 +00:00
|
|
|
|
|
|
|
tasks:
|
|
|
|
|
2012-10-27 21:55:35 +00:00
|
|
|
- name: "do this if my favcolor is blue, and my dog is named fido"
|
2012-03-20 23:55:24 +00:00
|
|
|
action: shell /bin/false
|
2012-10-27 21:55:35 +00:00
|
|
|
when_string: $favcolor == 'blue' and $dog == 'fido'
|
|
|
|
|
|
|
|
- name: "do this if my favcolor is not blue, and my dog is named fido"
|
|
|
|
action: shell /bin/true
|
|
|
|
when_string: $favcolor != 'blue' and $dog == 'fido'
|
|
|
|
|
|
|
|
- name: "do this if my SSN is over 9000"
|
|
|
|
action: shell /bin/true
|
|
|
|
when_integer: $ssn > 9000
|
2012-08-07 00:00:31 +00:00
|
|
|
|
2012-10-27 21:55:35 +00:00
|
|
|
- name: "do this if I have one of these SSNs"
|
|
|
|
action: shell /bin/true
|
|
|
|
when_integer: $ssn in [ 8675309, 8675310, 8675311 ]
|
|
|
|
|
|
|
|
- name: "do this if a variable named hippo is NOT defined"
|
2012-03-22 00:00:48 +00:00
|
|
|
action: shell /bin/true
|
2012-10-27 21:55:35 +00:00
|
|
|
when_unset: $hippo
|
|
|
|
|
|
|
|
- name: "do this if a variable named hippo is defined"
|
|
|
|
action: shell /bin/true
|
|
|
|
when_set: $hippo
|
|
|
|
|
2012-03-20 23:55:24 +00:00
|
|
|
|