|
6 | 6 | #
|
7 | 7 | - name: "Get guest network device manager"
|
8 | 8 | include_tasks: ../utils/get_network_manager.yml
|
| 9 | + when: >- |
| 10 | + guest_os_network_manager is undefined or |
| 11 | + not guest_os_network_manager |
9 | 12 |
|
10 |
| -- name: "Add network connection for {{ guest_os_ansible_distribution }}" |
| 13 | +- name: "Add network connection for new {{ adapter_type }} network adapter on {{ vm_guest_os_distribution }}" |
11 | 14 | when: guest_os_network_manager == "NetworkManager"
|
12 | 15 | block:
|
13 |
| - - name: "Add network connection with static IP address" |
14 |
| - block: |
15 |
| - - name: "Add a network connection with static IP address for {{ new_network_adapter }}" |
16 |
| - ansible.builtin.shell: >- |
17 |
| - nmcli connection add type ethernet |
18 |
| - ifname {{ new_network_adapter }} |
19 |
| - con-name {{ new_network_adapter }} |
20 |
| - ip4 {{ new_nic_ipv4 }}/{{ new_nic_net_prefix }} |
21 |
| - gw4 {{ new_nic_gateway }} |
22 |
| - delegate_to: "{{ vm_guest_ip }}" |
23 |
| - register: nm_add_static_conn |
24 |
| - |
25 |
| - - name: "Set result of adding network connection with static IP address" |
26 |
| - ansible.builtin.set_fact: |
27 |
| - nm_add_connection: "{{ nm_add_static_conn }}" |
28 |
| - when: |
29 |
| - - new_nic_ipv4 is defined and new_nic_ipv4 |
30 |
| - - new_nic_net_prefix is defined and new_nic_net_prefix |
31 |
| - - new_nic_gateway is defined and new_nic_gateway |
| 16 | + - name: "Set fact of IPv6 config in network connection for new {{ adapter_type }} network adapter" |
| 17 | + ansible.builtin.set_fact: |
| 18 | + nmcli_ipv6_method: >- |
| 19 | + {{ |
| 20 | + (guest_os_ansible_distribution == 'SLED' and |
| 21 | + guest_os_ansible_distribution_ver == '15.7') | |
| 22 | + ternary('ipv6.method disabled', '') |
| 23 | + }} |
| 24 | +
|
| 25 | + - name: "Set fact of command for adding network connection with static IP addresss" |
| 26 | + ansible.builtin.set_fact: |
| 27 | + nmcli_connection_add_cmd: >- |
| 28 | + nmcli connection add type ethernet ifname {{ new_network_adapter }} |
| 29 | + con-name {{ new_network_adapter }} ipv4.method manual |
| 30 | + ipv4.addresses {{ new_nic_ipv4 }}/{{ new_nic_net_prefix }} |
| 31 | + ipv4.gateway {{ new_nic_gateway }} {{ nmcli_ipv6_method }} |
| 32 | + when: new_nic_ipv4_method == 'static' |
| 33 | + |
| 34 | + - name: "Set fact of command for adding network connection with DHCP IP addresss" |
| 35 | + ansible.builtin.set_fact: |
| 36 | + nmcli_connection_add_cmd: >- |
| 37 | + nmcli connection add type ethernet ifname {{ new_network_adapter }} |
| 38 | + con-name {{ new_network_adapter }} {{ nmcli_ipv6_method }} |
| 39 | + when: new_nic_ipv4_method == 'dhcp' |
32 | 40 |
|
33 |
| - - name: "Add network connection with DHCP IP address" |
34 |
| - block: |
35 |
| - - name: "Add a network connection with DHCP IP address for {{ new_network_adapter }}" |
36 |
| - ansible.builtin.shell: >- |
37 |
| - nmcli connection add type ethernet |
38 |
| - ifname {{ new_network_adapter }} |
39 |
| - con-name {{ new_network_adapter }} |
40 |
| - delegate_to: "{{ vm_guest_ip }}" |
41 |
| - register: nm_add_dhcp_conn |
42 |
| - |
43 |
| - - name: "Set result of adding network connection with DHCP IP address" |
44 |
| - ansible.builtin.set_fact: |
45 |
| - nm_add_connection: "{{ nm_add_dhcp_conn }}" |
46 |
| - when: >- |
47 |
| - (new_nic_ipv4 is undefined or not new_nic_ipv4) or |
48 |
| - (new_nic_net_prefix is undefined or not new_nic_net_prefix) or |
49 |
| - (new_nic_gateway is undefined or not new_nic_gateway) |
50 |
| -
|
51 |
| - - name: "Check network connection is added for {{ new_network_adapter }}" |
52 |
| - ansible.builtin.assert: |
53 |
| - that: |
54 |
| - - nm_add_connection is defined |
55 |
| - - nm_add_connection.rc is defined |
56 |
| - - nm_add_connection.rc == 0 |
57 |
| - fail_msg: "Failed to add network connection on VM {{ vm_name }}" |
58 |
| - success_msg: "{{ nm_add_connection.stdout | |
59 |
| - default('Successfully added network connection on VM ' ~ vm_name) }}" |
| 41 | + - name: "Add a network connection for new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
| 42 | + ansible.builtin.shell: "{{ nmcli_connection_add_cmd }}" |
| 43 | + delegate_to: "{{ vm_guest_ip }}" |
| 44 | + register: nmcli_connection_add_result |
60 | 45 |
|
61 | 46 | # Set network config file in guest OS except VMware Photon OS
|
62 |
| -- name: "Set network config file for {{ guest_os_ansible_distribution }}" |
| 47 | +- name: "Set network config file for new {{ adapter_type }} network adapter on {{ vm_guest_os_distribution }}" |
63 | 48 | when:
|
64 | 49 | - guest_os_network_manager != "NetworkManager"
|
65 |
| - - guest_os_ansible_distribution not in ["VMware Photon OS", "Astra Linux (Orel)"] |
| 50 | + - guest_os_ansible_distribution not in ["VMware Photon OS", "FreeBSD", "Astra Linux (Orel)"] |
66 | 51 | block:
|
67 |
| - - name: "Initialize fact of network config tempate" |
| 52 | + - name: "Initialize fact of network config template" |
68 | 53 | ansible.builtin.set_fact:
|
69 | 54 | network_config_template: ""
|
70 | 55 |
|
71 |
| - - name: "Get network config file for '{{ new_network_adapter }}'" |
| 56 | + - name: "Get network config file for new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
72 | 57 | include_tasks: ../utils/get_network_config_file.yml
|
73 | 58 | vars:
|
74 | 59 | network_adapter_name: "{{ new_network_adapter }}"
|
75 | 60 |
|
76 |
| - - name: "Set fact of the network config template for {{ guest_os_ansible_distribution }}" |
77 |
| - ansible.builtin.set_fact: |
78 |
| - network_config_template: rhel_network_conf.j2 |
79 |
| - when: guest_os_family == "RedHat" |
80 |
| - |
81 |
| - - name: "Set fact of the network config template for {{ guest_os_ansible_distribution }}" |
82 |
| - ansible.builtin.set_fact: |
83 |
| - network_config_template: debian_network_conf.j2 |
84 |
| - when: > |
85 |
| - (guest_os_ansible_distribution == "Debian") or |
86 |
| - (guest_os_ansible_distribution == "Pardus GNU/Linux") or |
87 |
| - (guest_os_ansible_distribution == "Ubuntu" and |
88 |
| - guest_os_with_gui is defined and guest_os_with_gui) |
89 |
| -
|
90 |
| - - name: "Set fact of the network config template for Ubuntu server" |
91 |
| - ansible.builtin.set_fact: |
92 |
| - network_config_template: ubuntu_netplan_conf.j2 |
93 |
| - when: |
94 |
| - - guest_os_ansible_distribution == "Ubuntu" |
95 |
| - - guest_os_with_gui is defined and not guest_os_with_gui |
96 |
| - |
97 |
| - - name: "Set fact of the network config template for SLE" |
| 61 | + - name: "Set fact of the network config template on {{ vm_guest_os_distribution }}" |
98 | 62 | ansible.builtin.set_fact:
|
99 |
| - network_config_template: sles_network_conf.j2 |
100 |
| - when: guest_os_family == "Suse" |
| 63 | + network_config_template: "{{ guest_os_family | lower }}_network_conf.j2" |
| 64 | + when: guest_os_family in ["RedHat", "Suse", "Debian"] |
101 | 65 |
|
102 |
| - - name: "Set fact of the network config template for Flatcar" |
| 66 | + - name: "Set fact of the network config template on {{ vm_guest_os_distribution }}" |
103 | 67 | ansible.builtin.set_fact:
|
104 | 68 | network_config_template: flatcar_network_conf.j2
|
105 | 69 | when: guest_os_ansible_distribution == 'Flatcar'
|
106 | 70 |
|
107 |
| - - name: "Set fact of the network config template for FreeBSD" |
108 |
| - ansible.builtin.set_fact: |
109 |
| - network_config_template: freebsd_network_conf.j2 |
110 |
| - when: guest_os_family == "FreeBSD" |
111 |
| - |
112 | 71 | - name: "Create or update network config file for new network interface"
|
113 | 72 | when:
|
114 | 73 | - network_config_template
|
|
120 | 79 | dest: "{{ network_config_path }}"
|
121 | 80 | mode: "0666"
|
122 | 81 | delegate_to: "{{ vm_guest_ip }}"
|
123 |
| - |
| 82 | + |
124 | 83 | - name: "Get content of network config file '{{ network_config_path }}'"
|
125 | 84 | ansible.builtin.command: "cat {{ network_config_path }}"
|
126 | 85 | register: network_config
|
127 | 86 | changed_when: false
|
128 | 87 | delegate_to: "{{ vm_guest_ip }}"
|
129 |
| - |
| 88 | + |
130 | 89 | - name: "Print content of network config file '{{ network_config_path }}'"
|
131 | 90 | ansible.builtin.debug: var=network_config.stdout_lines
|
132 | 91 |
|
|
149 | 108 | seconds: 60
|
150 | 109 |
|
151 | 110 | # Update VM's guest IP
|
152 |
| - - include_tasks: ../../common/update_inventory.yml |
| 111 | + - name: "Update VM's guest IP" |
| 112 | + include_tasks: ../../common/update_inventory.yml |
153 | 113 |
|
154 |
| -- name: "Apply new network configuration file for FreeBSD" |
| 114 | +- name: "Apply new network config for FreeBSD" |
155 | 115 | when: guest_os_ansible_distribution == "FreeBSD"
|
156 | 116 | block:
|
157 |
| - - name: "Apply new vmxnet3 network configuration for FreeBSD" |
158 |
| - when: adapter_type == "vmxnet3" |
| 117 | + - name: "Set new {{ adapter_type }} network adapter with static IPv4 address" |
| 118 | + when: new_nic_ipv4_method == "static" |
159 | 119 | block:
|
160 |
| - - name: "Restart network" |
161 |
| - ansible.builtin.command: "/etc/netstart" |
| 120 | + - name: "Set new {{ adapter_type }} network adapter with static IPv4 address" |
| 121 | + ansible.builtin.shell: 'sysrc ifconfig_{{ new_network_adapter }}="inet {{ new_nic_ipv4 }}/{{ new_nic_net_prefix }}"' |
162 | 122 | delegate_to: "{{ vm_guest_ip }}"
|
163 |
| - async: 60 |
164 |
| - poll: 20 |
| 123 | + |
| 124 | + - name: "Restart network and routing on {{ vm_guest_os_distribution }}" |
| 125 | + ansible.builtin.shell: "service netif restart && service routing restart && dhclient {{ eth0_name }}" |
| 126 | + delegate_to: "{{ vm_guest_ip }}" |
| 127 | + async: 20 |
| 128 | + poll: 0 |
165 | 129 | ignore_errors: true
|
166 |
| - register: restart_network_result |
167 |
| - |
168 |
| - - name: "Display the result to restart network" |
169 |
| - ansible.builtin.debug: var=restart_network_result |
170 |
| - when: enable_debug is defined and enable_debug |
171 | 130 |
|
172 |
| - - name: "Update VM's guest IP after apply new network configuration" |
| 131 | + - name: "Sleep 60s for network restarting on {{ vm_guest_os_distribution }}" |
| 132 | + ansible.builtin.pause: |
| 133 | + seconds: 60 |
| 134 | + |
| 135 | + - name: "Update VM's guest IP in case it's changed" |
173 | 136 | include_tasks: ../../common/update_inventory.yml
|
174 | 137 |
|
175 |
| - - name: "Reboot to apply new e1000e network configuration for FreeBSD" |
176 |
| - include_tasks: ../utils/reboot.yml |
177 |
| - when: adapter_type == "e1000e" |
| 138 | + - name: "Set new {{ adapter_type }} network adapter with DHCP IPv4 address" |
| 139 | + when: new_nic_ipv4_method == "dhcp" |
| 140 | + block: |
| 141 | + - name: "Set new {{ adapter_type }} network adapter with static IPv4 address" |
| 142 | + ansible.builtin.shell: 'sysrc ifconfig_{{ new_network_adapter }}="DHCP"' |
| 143 | + delegate_to: "{{ vm_guest_ip }}" |
| 144 | + |
| 145 | + - name: "Get DHCP IPv4 address for new {{ adapter_type }} network adapter" |
| 146 | + ansible.builtin.shell: "dhclient {{ new_network_adapter }}" |
| 147 | + delegate_to: "{{ vm_guest_ip }}" |
178 | 148 |
|
179 |
| -- include_tasks: ../utils/set_network_adapter_status.yml |
| 149 | +- name: "Bring up the new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
| 150 | + include_tasks: ../utils/set_network_adapter_status.yml |
180 | 151 | vars:
|
181 | 152 | network_adapter_name: "{{ new_network_adapter }}"
|
182 | 153 | network_adapter_status: "up"
|
183 |
| - when: |
184 |
| - - guest_os_network_manager != "netplan" |
| 154 | + when: guest_os_network_manager != "netplan" |
185 | 155 |
|
186 | 156 | # Get IP address of new interface
|
187 | 157 | - name: "Set fact of searching IPv4 address"
|
188 | 158 | ansible.builtin.set_fact:
|
189 |
| - search_ip: "{% if adapter_type == 'pvrdma' %}{{ new_nic_ipv4 }}{% else %}192.168{% endif %}" |
| 159 | + search_ip: "{% if new_nic_ipv4 is defined and new_nic_ipv4 %}{{ new_nic_ipv4 }}{% else %}192.168{% endif %}" |
190 | 160 |
|
191 |
| -- name: "Set command to get link status for the new network adapter" |
| 161 | +- name: "Set command to get IP addresses of the new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
192 | 162 | ansible.builtin.set_fact:
|
193 | 163 | get_link_status_cmd: |-
|
194 | 164 | {%- if guest_os_ansible_distribution == 'FreeBSD' -%}ifconfig {{ new_network_adapter }} | grep inet | grep -v inet6 | awk '{print $2}'
|
195 | 165 | {%- else -%}ip -br addr show {{ new_network_adapter }} | awk '{print $3}'
|
196 | 166 | {%- endif -%}
|
197 | 167 |
|
198 |
| -- name: "Wait for network adapter '{{ new_network_adapter }}' IPv4 address" |
| 168 | +- name: "Wait for IPv4 address of the new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
199 | 169 | ansible.builtin.shell: "{{ get_link_status_cmd }}"
|
200 | 170 | register: link_status
|
201 | 171 | changed_when: false
|
|
208 | 178 | delegate_to: "{{ vm_guest_ip }}"
|
209 | 179 | ignore_errors: true
|
210 | 180 |
|
211 |
| -- name: "Check network adapter '{{ new_network_adapter }}' IPv4 address" |
| 181 | +- name: "Print IP addresses of the new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
| 182 | + ansible.builtin.debug: var=link_status |
| 183 | + |
| 184 | +- name: "Check IPv4 address of the new {{ adapter_type }} network adapter '{{ new_network_adapter }}'" |
212 | 185 | ansible.builtin.assert:
|
213 | 186 | that:
|
214 | 187 | - link_status is defined
|
215 | 188 | - link_status.stdout is defined
|
216 | 189 | - search_ip in link_status.stdout
|
217 | 190 | fail_msg: >-
|
218 |
| - Network adapter '{{ new_network_adapter }}' on VM {{ vm_name }} |
219 |
| - failed to obtain IPv4 address after 100 seconds. |
220 |
| - Its current IPv4 address is '{{ link_status.stdout | default("") }}'. |
| 191 | + The new {{ adapter_type }} network adapter '{{ new_network_adapter }}' |
| 192 | + failed to obtain IPv4 address in 100 seconds. The command '{{ get_link_status_cmd }}' |
| 193 | + returned '{{ link_status | default("") }}'. |
221 | 194 |
|
222 |
| -- name: "Print the new networ adapter IP address" |
223 |
| - ansible.builtin.debug: var=link_status |
| 195 | +- name: "Get the new {{ adapter_type }} newtwork adapter status after applying network config" |
| 196 | + include_tasks: ../utils/get_network_adapters_status.yml |
224 | 197 |
|
225 |
| -- include_tasks: ../utils/get_network_adapters_status.yml |
226 |
| -- name: "Update the new network adapter status" |
| 198 | +- name: "Update the new network adapter status after applying network config" |
227 | 199 | ansible.builtin.set_fact:
|
228 |
| - new_network_adapter_status: "{{ (guest_network_adapters_status | selectattr('device', 'equalto', new_network_adapter))[0] }}" |
| 200 | + new_network_adapter_status: "{{ guest_network_adapters_status | selectattr('device', 'equalto', new_network_adapter) | first }}" |
229 | 201 |
|
230 |
| -- name: "Print the new network adapter status after applying new network config" |
231 |
| - ansible.builtin.debug: var=new_network_adapter_status |
232 |
| - |
233 |
| -- name: "Set the new network adapter ip address after applying new network config" |
| 202 | +- name: "Set fact of the new {{ adapter_type }} network adapter's IPv4 address" |
234 | 203 | ansible.builtin.set_fact:
|
235 |
| - new_network_adapter_ip: "{{ link_status.stdout }}" |
| 204 | + new_network_adapter_ip: >- |
| 205 | + {{ |
| 206 | + (new_network_adapter_status.ipv4 | type_debug == "list") | |
| 207 | + ternary(new_network_adapter_status.ipv4[0].address | default('') , |
| 208 | + new_network_adapter_status.ipv4.address | default('')) |
| 209 | + }} |
| 210 | + when: |
| 211 | + - new_network_adapter_status.ipv4 is defined |
| 212 | + - new_network_adapter_status.ipv4 | type_debug in ['list', 'dict'] |
0 commit comments