166 lines
5.4 KiB
YAML
166 lines
5.4 KiB
YAML
---
|
|
- 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_detected: >-
|
|
{{ configuration_net_inf_from_facts | default(configuration_net_inf_from_ip, true) }}
|
|
configuration_net_inf_regex: "{{ configuration_net_inf_detected | 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_detected, {}).get('macaddress', '')
|
|
)
|
|
| default(
|
|
(ansible_facts | default({})).get('ansible_' + configuration_net_inf_detected, {}).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_detected }}"
|
|
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: Configure NetworkManager profile
|
|
when: os | lower not in ["alpine", "void"]
|
|
block:
|
|
- 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"
|
|
|
|
- name: Configure Alpine networking
|
|
when: os | lower == "alpine"
|
|
vars:
|
|
configuration_dns_list: "{{ system_cfg.dns.servers | default([]) }}"
|
|
configuration_alpine_static: >-
|
|
{{
|
|
system_cfg.ip is defined
|
|
and system_cfg.ip | string | length > 0
|
|
and system_cfg.prefix is defined
|
|
and (system_cfg.prefix | string | length) > 0
|
|
}}
|
|
block:
|
|
- name: Write Alpine network interfaces
|
|
ansible.builtin.copy:
|
|
dest: /mnt/etc/network/interfaces
|
|
mode: "0644"
|
|
content: |
|
|
auto lo
|
|
iface lo inet loopback
|
|
|
|
auto {{ configuration_net_inf }}
|
|
iface {{ configuration_net_inf }} inet {{ 'static' if configuration_alpine_static | bool else 'dhcp' }}
|
|
{% if configuration_alpine_static | bool %}
|
|
address {{ system_cfg.ip }}/{{ system_cfg.prefix }}
|
|
{% if system_cfg.gateway is defined and system_cfg.gateway | string | length %}
|
|
gateway {{ system_cfg.gateway }}
|
|
{% endif %}
|
|
{% endif %}
|
|
|
|
- name: Set Alpine DNS resolvers
|
|
when: configuration_dns_list | length > 0
|
|
ansible.builtin.copy:
|
|
dest: /mnt/etc/resolv.conf
|
|
mode: "0644"
|
|
content: |
|
|
{% for resolver in configuration_dns_list %}
|
|
nameserver {{ resolver }}
|
|
{% endfor %}
|
|
|
|
- name: Configure Void networking
|
|
when: os | lower == "void"
|
|
vars:
|
|
configuration_dns_list: "{{ system_cfg.dns.servers | default([]) }}"
|
|
configuration_void_static: >-
|
|
{{
|
|
system_cfg.ip is defined
|
|
and system_cfg.ip | string | length > 0
|
|
and system_cfg.prefix is defined
|
|
and (system_cfg.prefix | string | length) > 0
|
|
}}
|
|
block:
|
|
- name: Write dhcpcd configuration for static networking
|
|
when: configuration_void_static | bool
|
|
ansible.builtin.copy:
|
|
dest: /mnt/etc/dhcpcd.conf
|
|
mode: "0644"
|
|
content: |
|
|
interface {{ configuration_net_inf }}
|
|
static ip_address={{ system_cfg.ip }}/{{ system_cfg.prefix }}
|
|
{% if system_cfg.gateway is defined and system_cfg.gateway | string | length %}
|
|
static routers={{ system_cfg.gateway }}
|
|
{% endif %}
|
|
{% if configuration_dns_list | length > 0 %}
|
|
static domain_name_servers={{ configuration_dns_list | join(' ') }}
|
|
{% endif %}
|