-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_conditions_and_loops.yaml
50 lines (42 loc) · 1.44 KB
/
07_conditions_and_loops.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---
# can you conditionally run a task ?
# of course, let's throw in a loop and only print
# the family if the father is named "john"
- name: print only John's family
hosts: localhost
vars_files:
- vars/families.yaml
vars:
an_undefined_variable:
a_defined_variable: "I am defined"
tasks:
- name: print only John's family
debug:
var: family
# only print the item when the condition is true
when: item.father == 'John'
# loop over the families
with_items: "{{ families }}"
# loop control is optional but can be useful
loop_control:
loop_variable: family
label: "{{ family.father }}'s family has {{ family.children | length }} children"
# can we put a condition if a variable contains a value ? (is defined)
- name: print when a variable is defined
debug:
msg: "I am defined"
when: a_defined_variable is defined
# can we do this if it does not contain a value ? (undefined / or null)
- name: print when a variable is not defined
debug:
msg: "I am not defined"
when: an_undefined_variable is not defined
# now lets inverse the last two tasks logically
- name: skip when a variable is not defined
debug:
msg: "I am not defined"
when: a_defined_variable is not defined
- name: skip when a variable is defined
debug:
msg: "I am defined"
when: an_undefined_variable is defined