diff --git a/roles/configuration/tasks/network.yml b/roles/configuration/tasks/network.yml index 1494123..23dbc53 100644 --- a/roles/configuration/tasks/network.yml +++ b/roles/configuration/tasks/network.yml @@ -29,97 +29,9 @@ - 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: >- - {{ - item.name - if (item.name | default('') | string | length) > 0 - else (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" +- name: Configure networking 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 inv_name = iface.name | default('') | string %} - {% set det_name = configuration_detected_interfaces[loop.index0] | default('eth' ~ loop.index0) %} - {% set iface_name = inv_name if inv_name | length > 0 else det_name %} - {% 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 inv_name = iface.name | default('') | string %} - {% set det_name = configuration_detected_interfaces[loop.index0] | default('eth' ~ loop.index0) %} - {% set iface_name = inv_name if inv_name | length > 0 else det_name %} - {% 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 %} + configuration_network_task_map: + alpine: network_alpine.yml + void: network_void.yml + ansible.builtin.include_tasks: "{{ configuration_network_task_map[os] | default('network_nm.yml') }}" diff --git a/roles/configuration/tasks/network_alpine.yml b/roles/configuration/tasks/network_alpine.yml new file mode 100644 index 0000000..fd1f76b --- /dev/null +++ b/roles/configuration/tasks/network_alpine.yml @@ -0,0 +1,37 @@ +--- +- name: Write Alpine network interfaces + vars: + configuration_dns_list: "{{ system_cfg.network.dns.servers | default([]) }}" + 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 inv_name = iface.name | default('') | string %} + {% set det_name = configuration_detected_interfaces[loop.index0] | default('eth' ~ loop.index0) %} + {% set iface_name = inv_name if inv_name | length > 0 else det_name %} + {% 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 + vars: + configuration_dns_list: "{{ system_cfg.network.dns.servers | default([]) }}" + 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 %} diff --git a/roles/configuration/tasks/network_nm.yml b/roles/configuration/tasks/network_nm.yml new file mode 100644 index 0000000..f2d8667 --- /dev/null +++ b/roles/configuration/tasks/network_nm.yml @@ -0,0 +1,26 @@ +--- +- name: Copy NetworkManager keyfile per interface + vars: + configuration_iface: "{{ item }}" + configuration_iface_name: >- + {{ + item.name + if (item.name | default('') | string | length) > 0 + else (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" diff --git a/roles/configuration/tasks/network_void.yml b/roles/configuration/tasks/network_void.yml new file mode 100644 index 0000000..f7bbe54 --- /dev/null +++ b/roles/configuration/tasks/network_void.yml @@ -0,0 +1,25 @@ +--- +- name: Write dhcpcd configuration + vars: + configuration_dns_list: "{{ system_cfg.network.dns.servers | default([]) }}" + ansible.builtin.copy: + dest: /mnt/etc/dhcpcd.conf + mode: "0644" + content: | + {% for iface in system_cfg.network.interfaces %} + {% set inv_name = iface.name | default('') | string %} + {% set det_name = configuration_detected_interfaces[loop.index0] | default('eth' ~ loop.index0) %} + {% set iface_name = inv_name if inv_name | length > 0 else det_name %} + {% 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 %}