/
main.yml
279 lines (255 loc) · 9.02 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
---
# NOTE: Handlers are executed in the order in which they are defined, not the
# order in which they are notified.
# Some systems do not support /etc/modules-load.d, in which case we fall back
# to /etc/modules.
- name: Check whether /etc/modules-load.d exists
become: true
stat:
path: /etc/modules-load.d
register: modules_load_stat
- name: Make sure the bonding module is loaded
become: true
modprobe:
name: bonding
state: present
- name: Make sure the bonding module is loaded at boot via /etc/modules-load.d
become: true
template:
src: module.conf.j2
dest: /etc/modules-load.d/bonding.conf
when: modules_load_stat.stat.exists
vars:
module_name: bonding
- name: Make sure the bonding module is loaded at boot via /etc/modules
become: true
lineinfile:
dest: /etc/modules
line: bonding
when: not modules_load_stat.stat.exists
- name: Make sure the 8021q module is loaded
become: true
modprobe:
name: 8021q
state: present
- name: Make sure the 8021q module is loaded at boot via /etc/modules-load.d
become: true
template:
src: module.conf.j2
dest: /etc/modules-load.d/8021q.conf
when: modules_load_stat.stat.exists
vars:
module_name: 8021q
- name: Make sure the 8021q module is loaded at boot via /etc/modules
become: true
lineinfile:
dest: /etc/modules
line: 8021q
when: not modules_load_stat.stat.exists
# We need to bounce the changed network interfaces carefully, as some
# interfaces may depend on others. To further complicate matters, the
# dependencies are different between RedHat and Debian systems. Here are the
# possible dependencies between interface types (X -> Y means X depends on Y):
#
# Ethernet (VLAN) -> Ethernet
# Ethernet (VLAN) -> bond master
# Ethernet (VLAN) -> bridge
# [RedHat] Bond master -> bridge
# [RedHat] Bond slave -> bond master
# [Debian] Bond master -> bond slave
# Bond slave (VLAN) -> Ethernet
# [RedHat] bridge port -> bridge
# [Debian] bridge -> bridge port
# [Debian] bridge -> bond master
# Bridge port (VLAN) -> Ethernet
# Bridge port (VLAN) -> bond master
# Bridge port (VLAN) -> bridge
#
# Note that VLAN interfaces as bond slaves do not appear to work on Debian.
#
# This generates a number of valid orderings. For RedHat, we'll go with this
# one:
# - Ethernet (non VLAN)
# - Bridge
# - Bond master
# - Bridge port
# - Bond slave
# - Ethernet (VLAN)
#
# And for Debian, this one:
#
# - Ethernet (non VLAN)
# - Bond slave
# - Bond master
# - Bridge port (non VLAN)
# - Bridge
# - Bridge port (VLAN)
# - Ethernet (VLAN)
#
# Also, use nohup to ensure the process survives termination of the SSH
# connection.
#
# Note on "--allow" flag for Debian systems:
#
# On Debian system the "--allow" flag to "ifup/ifdown" behaves like a filter
# to the interfaces configured in /etc/network/interfaces. On Debian you
# cannot bounce an interface marked with "allow-hotplug" (e.g. a hot-pluggable
# wifi interface) using "ifup/ifdown", it will fail. Adding the "--allow auto"
# argument to "ifup/ifdown" will only be effective for interfaces marked with
# "auto". Interfaces marked with "allow-hotplug" will simply be ignored then.
#
# Note on Network Manager Bounce mechanism:
#
# 'nmcli connection up' works asynchronously so we need to check if interfaces
# are truly up, as gathering facts in next step would fail due to inactive
# state. Running another loop to check that.
# Dummy interfaces can have UNKNOWN state.
- name: Bounce networkmanager devices
become: true
command: >
nohup bash -c "
nmcli connection reload;
returncode=0
{% for interface in all_interfaces_changed | reverse %}
nmcli connection down {{ interface }};
{% endfor %}
{% for interface in all_interfaces_changed %}
if ! nmcli connection up {{ interface }}; then
echo \"Failed to bring up interface {{ interface }}\";
returncode=1
fi;
{% endfor %}
echo 'Waiting for interfaces to become active and have a connected carrier';
for i in {1..20}; do
all_connected=true
{% for interface in all_interfaces_changed %}
if [[ '{{ interface }}' == *dummy* ]]; then
allowed_states='UNKNOWN|UP'
else
allowed_states='UP'
fi
if ! (nmcli -t -f DEVICE,STATE device status | grep -q -w '{{ interface }}:connected' && ip link show '{{ interface }}' | grep -q -E 'state ('$allowed_states')'); then
all_connected=false
if [ $i -eq 20 ]; then
echo 'Interface {{ interface }} failed to become active and have a connected carrier';
returncode=1
fi
fi;
{% endfor %}
if $all_connected; then
break;
fi
sleep 2;
done
exit $returncode"
vars:
ether_vlan_interfaces_changed: >
{{ ether_interfaces_changed | select('match', vlan_interface_regex) | list}}
ether_non_vlan_interfaces_changed: >
{{ ether_interfaces_changed | reject('match', vlan_interface_regex) | list }}
bridge_port_vlan_interfaces_changed: >
{{ bridge_port_interfaces_changed | select('match', vlan_interface_regex) | list}}
bridge_port_non_vlan_interfaces_changed: >
{{ bridge_port_interfaces_changed | reject('match', vlan_interface_regex) | list }}
all_interfaces_changed: >
{{ ether_non_vlan_interfaces_changed +
bridge_interfaces_changed +
bond_master_interfaces_changed +
bridge_port_interfaces_changed +
bond_slave_interfaces_changed +
ether_vlan_interfaces_changed }}
listen: Bounce network devices
when: interfaces_use_networkmanager
notify:
- Gather facts
- Check active Ethernet interface state
- Check active bond interface state
- Check active bridge interface state
# The order handlers execute are based on the order they're arranged here.
# We want to do a controlled bounce of interfaces before we do any restart.
- name: Restart NetworkManager
become: true
service:
name: NetworkManager
state: restarted
- name: Bounce network devices
become: true
command: >
nohup bash -c "
returncode=0
{% for interface in all_interfaces_changed | reverse %}
ifdown {% if ansible_facts.os_family == 'Debian' %}--allow auto {% endif %}{{ interface }};
{% endfor %}
{% for interface in all_interfaces_changed %}
if ! ifup {% if ansible_facts.os_family == 'Debian' %}--allow auto {% endif %}{{ interface }}; then
echo \"Failed to bring up interface {{ interface }}\";
returncode=1
fi;
{% endfor %}
exit $returncode"
vars:
ether_vlan_interfaces_changed: >
{{ ether_interfaces_changed | select('match', vlan_interface_regex) | list}}
ether_non_vlan_interfaces_changed: >
{{ ether_interfaces_changed | reject('match', vlan_interface_regex) | list }}
bridge_port_vlan_interfaces_changed: >
{{ bridge_port_interfaces_changed | select('match', vlan_interface_regex) | list}}
bridge_port_non_vlan_interfaces_changed: >
{{ bridge_port_interfaces_changed | reject('match', vlan_interface_regex) | list }}
all_interfaces_changed_map:
Debian: >
{{ ether_non_vlan_interfaces_changed +
bond_slave_interfaces_changed +
bond_master_interfaces_changed +
bridge_port_non_vlan_interfaces_changed +
bridge_interfaces_changed +
bridge_port_vlan_interfaces_changed +
ether_vlan_interfaces_changed }}
# On RedHat, bounce any changed bond master interfaces for a second time.
# The reason for this is unclear.
RedHat: >
{{ ether_non_vlan_interfaces_changed +
bridge_interfaces_changed +
bond_master_interfaces_changed +
bridge_port_interfaces_changed +
bond_slave_interfaces_changed +
ether_vlan_interfaces_changed +
bond_master_interfaces_changed }}
all_interfaces_changed: "{{ all_interfaces_changed_map[ansible_facts.os_family] }}"
when: not interfaces_use_networkmanager
notify:
- Pause to wait for interfaces to become active
- Gather facts
- Check active Ethernet interface state
- Check active bond interface state
- Check active bridge interface state
- name: Pause to wait for interfaces to become active
pause:
seconds: "{{ interfaces_pause_time }}"
# Update host facts so that we can verify the configuration has been applied
# correctly.
- name: Gather facts
setup:
filter: "{{ interfaces_setup_filter }}"
gather_subset: "{{ interfaces_setup_gather_subset }}"
- name: Check active Ethernet interface state
fail:
msg: "{{ ether_check.reason }}"
with_items: "{{ interfaces_ether_interfaces }}"
when: ether_check.diff
vars:
ether_check: "{{ item | ether_check }}"
- name: Check active bond interface state
fail:
msg: "{{ bond_check.reason }}"
with_items: "{{ interfaces_bond_interfaces }}"
when: bond_check.diff
vars:
bond_check: "{{ item | bond_check }}"
- name: Check active bridge interface state
fail:
msg: "{{ bridge_check.reason }}"
with_items: "{{ interfaces_bridge_interfaces }}"
when: bridge_check.diff
vars:
bridge_check: "{{ item | bridge_check }}"