--- - name: Read network interfaces ansible.builtin.command: argv: - ip - -o - link - show register: configuration_ip_link changed_when: false failed_when: false - name: Detect available network interface names vars: configuration_detected_interfaces: >- {{ configuration_ip_link.stdout | default('') | regex_findall('^[0-9]+: ([^:]+):', multiline=True) | reject('equalto', 'lo') | list }} ansible.builtin.set_fact: configuration_detected_interfaces: "{{ configuration_detected_interfaces }}" - name: Validate at least one network interface detected ansible.builtin.assert: that: - configuration_detected_interfaces | length > 0 fail_msg: Failed to detect any network interfaces. - name: Configure NetworkManager profiles when: os not in ["alpine", "void"] block: - name: Copy NetworkManager keyfile per interface vars: configuration_iface: "{{ item }}" configuration_iface_name: "{{ configuration_detected_interfaces[idx] | default('eth' ~ idx) }}" configuration_net_uuid: "{{ ('LAN-' ~ idx ~ '-' ~ hostname) | ansible.builtin.to_uuid }}" ansible.builtin.template: src: network.j2 dest: "/mnt/etc/NetworkManager/system-connections/LAN-{{ idx }}.nmconnection" mode: "0600" loop: "{{ system_cfg.network.interfaces }}" loop_control: index_var: idx label: "LAN-{{ idx }}" - name: Fix Ubuntu unmanaged devices when: os 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 == "alpine" vars: configuration_dns_list: "{{ system_cfg.network.dns.servers | default([]) }}" block: - name: Write Alpine network interfaces ansible.builtin.copy: dest: /mnt/etc/network/interfaces mode: "0644" content: | auto lo iface lo inet loopback {% for iface in system_cfg.network.interfaces %} {% set iface_name = configuration_detected_interfaces[loop.index0] | default(iface.name | default('eth' ~ loop.index0)) %} {% set has_static = (iface.ip | default('') | string | length) > 0 %} auto {{ iface_name }} iface {{ iface_name }} inet {{ 'static' if has_static else 'dhcp' }} {% if has_static %} address {{ iface.ip }}/{{ iface.prefix }} {% if iface.gateway | default('') | string | length %} gateway {{ iface.gateway }} {% endif %} {% endif %} {% endfor %} - 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 == "void" vars: configuration_dns_list: "{{ system_cfg.network.dns.servers | default([]) }}" block: - name: Write dhcpcd configuration ansible.builtin.copy: dest: /mnt/etc/dhcpcd.conf mode: "0644" content: | {% for iface in system_cfg.network.interfaces %} {% set iface_name = configuration_detected_interfaces[loop.index0] | default(iface.name | default('eth' ~ loop.index0)) %} {% set has_static = (iface.ip | default('') | string | length) > 0 %} {% if has_static %} interface {{ iface_name }} static ip_address={{ iface.ip }}/{{ iface.prefix }} {% if iface.gateway | default('') | string | length %} static routers={{ iface.gateway }} {% endif %} {% if loop.index0 == 0 and configuration_dns_list | length > 0 %} static domain_name_servers={{ configuration_dns_list | join(' ') }} {% endif %} {% endif %} {% endfor %}