ansible/examples/playbooks/conditionals_part2.yml

51 lines
1.4 KiB
YAML
Raw Normal View History

---
# this is a demo of conditional executions using 'when' statements, which can skip
# certain tasks on machines/platforms/etc where they do not apply.
- hosts: all
user: root
vars:
favcolor: "red"
dog: "fido"
cat: "whiskers"
ssn: 8675309
# These are the types of when statemnets available
# when_set: $variable_name
# when_unset: $variable_name
# when_str: $x == "test"
# when_int: $y > 2
# when_float: $z => 2.3
#
# 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"
tasks:
- name: "do this if my favcolor is blue, and my dog is named fido"
action: shell /bin/false
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 02:00:31 +02: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"
action: shell /bin/true
when_unset: $hippo
- name: "do this if a variable named hippo is defined"
action: shell /bin/true
when_set: $hippo