Configuration role refactor and network template
This commit is contained in:
96
roles/configuration/tasks/network.yml
Normal file
96
roles/configuration/tasks/network.yml
Normal file
@@ -0,0 +1,96 @@
|
||||
---
|
||||
- name: Generate UUID for Network Profile
|
||||
ansible.builtin.set_fact:
|
||||
configuration_net_uuid: "{{ ('LAN-' ~ hostname) | ansible.builtin.to_uuid }}"
|
||||
changed_when: false
|
||||
|
||||
- name: Read network interfaces
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- ip
|
||||
- -o
|
||||
- link
|
||||
- show
|
||||
register: configuration_ip_link
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
|
||||
- name: Resolve network interface and MAC address
|
||||
vars:
|
||||
configuration_net_inf_from_facts: "{{ (ansible_default_ipv4 | default({})).get('interface', '') }}"
|
||||
configuration_net_inf_from_ip: >-
|
||||
{{
|
||||
(
|
||||
configuration_ip_link.stdout
|
||||
| default('')
|
||||
| regex_findall('^[0-9]+: ([^:]+):', multiline=True)
|
||||
| reject('equalto', 'lo')
|
||||
| list
|
||||
| first
|
||||
)
|
||||
| default('')
|
||||
}}
|
||||
configuration_net_inf_effective: >-
|
||||
{{ configuration_net_inf_from_facts | default(configuration_net_inf_from_ip, true) }}
|
||||
configuration_net_inf_regex: "{{ configuration_net_inf_effective | ansible.builtin.regex_escape }}"
|
||||
configuration_net_mac_from_virtualization: "{{ virtualization_mac_address | default('') }}"
|
||||
configuration_net_mac_from_facts: >-
|
||||
{{
|
||||
(
|
||||
(ansible_facts | default({})).get(configuration_net_inf_effective, {}).get('macaddress', '')
|
||||
)
|
||||
| default(
|
||||
(ansible_facts | default({})).get('ansible_' + configuration_net_inf_effective, {}).get('macaddress', ''),
|
||||
true
|
||||
)
|
||||
}}
|
||||
configuration_net_mac_from_ip: >-
|
||||
{{
|
||||
(
|
||||
configuration_ip_link.stdout
|
||||
| default('')
|
||||
| regex_findall(
|
||||
'^\\d+: ' ~ configuration_net_inf_regex ~ ':.*?link/ether\\s+([0-9A-Fa-f:]{17})',
|
||||
multiline=True
|
||||
)
|
||||
| first
|
||||
)
|
||||
| default('')
|
||||
}}
|
||||
ansible.builtin.set_fact:
|
||||
configuration_net_inf: "{{ configuration_net_inf_effective }}"
|
||||
configuration_net_mac: >-
|
||||
{{
|
||||
(
|
||||
configuration_net_mac_from_virtualization
|
||||
| default(configuration_net_mac_from_facts, true)
|
||||
| default(configuration_net_mac_from_ip, true)
|
||||
)
|
||||
| upper
|
||||
}}
|
||||
changed_when: false
|
||||
|
||||
- name: Validate Network Interface Name
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- configuration_net_inf | length > 0
|
||||
fail_msg: Failed to detect an active network interface.
|
||||
|
||||
- name: Validate Network Interface MAC Address
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- configuration_net_mac | length > 0
|
||||
fail_msg: Failed to detect the MAC address for network interface {{ configuration_net_inf }}.
|
||||
|
||||
- name: Copy NetworkManager keyfile
|
||||
ansible.builtin.template:
|
||||
src: network.j2
|
||||
dest: /mnt/etc/NetworkManager/system-connections/LAN.nmconnection
|
||||
mode: "0600"
|
||||
|
||||
- name: Fix Ubuntu unmanaged devices
|
||||
when: os | lower in ["ubuntu", "ubuntu-lts"]
|
||||
ansible.builtin.file:
|
||||
path: /mnt/etc/NetworkManager/conf.d/10-globally-managed-devices.conf
|
||||
state: touch
|
||||
mode: "0644"
|
||||
Reference in New Issue
Block a user