-
Notifications
You must be signed in to change notification settings - Fork 426
/
dynamic_inventory.yml
143 lines (122 loc) · 3.19 KB
/
dynamic_inventory.yml
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
- name: add hosts to inventory
hosts: all
vars:
num_hosts: 10
tasks:
- name: create inventory
add_host:
name: 'host-{{item}}'
groups: dynamic
ansible_connection: local
host_id: '{{item}}'
with_sequence: start=1 end={{num_hosts}} format=%d
notify:
- single host handler
handlers:
- name: single host handler
command: 'true'
- name: various task responses on dynamic inventory
hosts: 'dynamic'
tasks:
- name: fail even numbered hosts
command: 'false'
when: host_id|int % 2 == 0
- name: skip multiples of 3
command: 'true'
when: host_id|int % 3 != 0
- name: all skipped
command: 'true'
when: false
- name: all changed
command: 'true'
notify:
- changed handler
- name: all ok
command: 'true'
changed_when: false
notify:
# this will never notify since the task never is changed: true
- ok handler
handlers:
- name: ok handler
command: 'false'
- name: changed handler
command: 'true'
notify: another changed handler
- name: yet another changed handler
command: 'true'
- name: another changed handler
command: 'true'
- name: shrink inventory to 1 host
hosts: 'dynamic'
tasks:
- name: fail all but 1
command: 'false'
when: host_id|int != 1
- name: 1 host play
hosts: 'host-1'
gather_facts: false
tasks:
- name: pass
command: 'true'
- name: ok
command: 'true'
changed_when: false
- name: ignored
command: 'false'
ignore_errors: true
- name: fail remaining dynamic group
hosts: all
gather_facts: true
tasks:
- name: fail
command: 'false'
when: inventory_hostname == 'host-1'
- name: add hosts to inventory
hosts: all
vars:
num_hosts: 10
tasks:
- name: add dynamic inventory
add_host:
name: 'new-host-{{item}}'
groups: dynamic
ansible_connection: local
host_id: '{{item}}'
with_sequence: start=1 end={{(num_hosts|int / 2)|int}} format=%d
- name: add unreachable inventory
add_host:
name: 'unreachable-host-{{item}}'
groups: unreachable
ansible_connection: ssh
host_id: '{{item}}'
with_sequence: start={{(num_hosts|int / 2)|int + 1}} end={{num_hosts}} format=%d
- name: add more_unreachable inventory
add_host:
name: 'more-unreachable-host-{{item}}'
groups: more-unreachable
ansible_connection: ssh
host_id: '{{item}}'
with_sequence: start={{(num_hosts|int / 2)|int + 1}} end={{num_hosts}} format=%d
- name: some unreachable, some changed, some skipped
hosts: 'dynamic:unreachable'
gather_facts: false
tasks:
- name: all changed with items
command: 'true'
when: host_id|int % 5 == 0
- name: no matching hosts
hosts: no_hosts_will_ever_match_this_string
- name: gather facts fail
hosts: unreachable
gather_facts: true
- name: some gather facts fail
hosts: more-unreachable:dynamic
gather_facts: true
tasks:
- name: skip this task
command: 'false'
when: false
- name: no tasks, just the facts
hosts: all