From 6108475fbd06e045e285d925138b0c81441badba Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 14 Apr 2024 17:23:27 -0500 Subject: [PATCH] Refactor netplan --- roles/network/defaults/main.yml | 18 ++++++++++++++++++ roles/network/tasks/netplan.yml | 8 ++++---- roles/network/templates/netplan.yaml.j2 | 15 +++++++++------ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/roles/network/defaults/main.yml b/roles/network/defaults/main.yml index ccafadf..35154c7 100644 --- a/roles/network/defaults/main.yml +++ b/roles/network/defaults/main.yml @@ -6,6 +6,23 @@ network_netplan_config_path: "{{ network_netplan_etc_path }}/ansible.yaml" network_netplan_default_config_path: "{{ network_netplan_etc_path }}/01-netcfg.yaml" # network_netplan_default_config_state: absent +network_netplan: + network: + version: 2 + ethernets: + eth0: + dhcp4: false + dhcp6: false + accept-ra: true + addresses: + - "{{ ansible_default_ipv4.address }}/{{ (ansible_default_ipv4.network + '/' + ansible_default_ipv4.netmask) | ansible.utils.ipaddr('prefix') }}" + - "{{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }}" + routes: + - to: default + via: "{{ ansible_default_ipv4.gateway }}" + nameservers: + addresses: "{{ network_dns_nameservers }}" + network_interfaces: - name: eth0 inet4: @@ -15,6 +32,7 @@ network_interfaces: gateway: "{{ ansible_default_ipv4.gateway }}" inet6: dhcp: false + accept_ra: true address: - "{{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }}" gateway: "{{ ansible_default_ipv6.gateway }}" diff --git a/roles/network/tasks/netplan.yml b/roles/network/tasks/netplan.yml index 9d052f9..650e988 100644 --- a/roles/network/tasks/netplan.yml +++ b/roles/network/tasks/netplan.yml @@ -5,14 +5,14 @@ state: "{{ network_netplan_default_config_state | default('absent') }}" owner: root group: root - mode: 0644 + mode: '0400' notify: netplan apply - name: Configure netplan - ansible.builtin.template: + ansible.builtin.copy: dest: "{{ network_netplan_config_path }}" - src: netplan.yaml.j2 + content: "{{ network_netplan | to_nice_yaml }}" owner: root group: root - mode: '0644' + mode: '0400' notify: netplan apply diff --git a/roles/network/templates/netplan.yaml.j2 b/roles/network/templates/netplan.yaml.j2 index a7ecacf..95ff58d 100644 --- a/roles/network/templates/netplan.yaml.j2 +++ b/roles/network/templates/netplan.yaml.j2 @@ -1,16 +1,19 @@ --- network: - version: "{{ network_netplan_version | default(2) }}" - renderer: "{{ network_netplan_renderer | default("networkd") }}" + version: {{ network_netplan_version | default(2) }} + renderer: {{ network_netplan_renderer | default('networkd') }} {% if network_interfaces is defined and network_interfaces | length %} ethernets: {% for iface in network_interfaces %} {{ iface['name'] }}: {% if iface['inet4']['dhcp'] is defined %} - dhcp4: "{{ iface['inet4']['dhcp'] | ternary('yes', 'no') }}" + dhcp4: {{ iface['inet4']['dhcp'] | ternary('true', 'false') }} {% endif %} {% if iface['inet4']['dhcp'] is defined %} - dhcp6: "{{ iface['inet6']['dhcp'] | ternary('yes', 'no') }}" + dhcp6: {{ iface['inet6']['dhcp'] | ternary('true', 'false') }} +{% endif %} +{% if iface['inet6']['accept_ra'] is defined %} + accept-ra: {{ iface['inet6']['accept_ra'] | ternary('true', 'false') }} {% endif %} {% if iface['inet4']['address'] is defined or iface['inet6']['address'] is defined %} addresses: @@ -22,10 +25,10 @@ network: {% endfor %} {% endif %} {% if iface['inet4']['gateway'] is defined %} - gateway4: "{{ iface['inet4']['gateway'] }}" + gateway4: {{ iface['inet4']['gateway'] }} {% endif %} {% if iface['inet6']['gateway'] is defined %} - gateway6: "{{ iface['inet6']['gateway'] }}" + gateway6: {{ iface['inet6']['gateway'] }} {% endif %} {% if network_dns_nameservers is defined %} nameservers: