Refactor netplan

This commit is contained in:
Ryan Cavicchioni 2024-04-14 17:23:27 -05:00
parent db8c7f4f63
commit 6108475fbd
3 changed files with 31 additions and 10 deletions

View File

@ -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_path: "{{ network_netplan_etc_path }}/01-netcfg.yaml"
# network_netplan_default_config_state: absent # 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: network_interfaces:
- name: eth0 - name: eth0
inet4: inet4:
@ -15,6 +32,7 @@ network_interfaces:
gateway: "{{ ansible_default_ipv4.gateway }}" gateway: "{{ ansible_default_ipv4.gateway }}"
inet6: inet6:
dhcp: false dhcp: false
accept_ra: true
address: address:
- "{{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }}" - "{{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }}"
gateway: "{{ ansible_default_ipv6.gateway }}" gateway: "{{ ansible_default_ipv6.gateway }}"

View File

@ -5,14 +5,14 @@
state: "{{ network_netplan_default_config_state | default('absent') }}" state: "{{ network_netplan_default_config_state | default('absent') }}"
owner: root owner: root
group: root group: root
mode: 0644 mode: '0400'
notify: netplan apply notify: netplan apply
- name: Configure netplan - name: Configure netplan
ansible.builtin.template: ansible.builtin.copy:
dest: "{{ network_netplan_config_path }}" dest: "{{ network_netplan_config_path }}"
src: netplan.yaml.j2 content: "{{ network_netplan | to_nice_yaml }}"
owner: root owner: root
group: root group: root
mode: '0644' mode: '0400'
notify: netplan apply notify: netplan apply

View File

@ -1,16 +1,19 @@
--- ---
network: network:
version: "{{ network_netplan_version | default(2) }}" version: {{ network_netplan_version | default(2) }}
renderer: "{{ network_netplan_renderer | default("networkd") }}" renderer: {{ network_netplan_renderer | default('networkd') }}
{% if network_interfaces is defined and network_interfaces | length %} {% if network_interfaces is defined and network_interfaces | length %}
ethernets: ethernets:
{% for iface in network_interfaces %} {% for iface in network_interfaces %}
{{ iface['name'] }}: {{ iface['name'] }}:
{% if iface['inet4']['dhcp'] is defined %} {% if iface['inet4']['dhcp'] is defined %}
dhcp4: "{{ iface['inet4']['dhcp'] | ternary('yes', 'no') }}" dhcp4: {{ iface['inet4']['dhcp'] | ternary('true', 'false') }}
{% endif %} {% endif %}
{% if iface['inet4']['dhcp'] is defined %} {% 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 %} {% endif %}
{% if iface['inet4']['address'] is defined or iface['inet6']['address'] is defined %} {% if iface['inet4']['address'] is defined or iface['inet6']['address'] is defined %}
addresses: addresses:
@ -22,10 +25,10 @@ network:
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% if iface['inet4']['gateway'] is defined %} {% if iface['inet4']['gateway'] is defined %}
gateway4: "{{ iface['inet4']['gateway'] }}" gateway4: {{ iface['inet4']['gateway'] }}
{% endif %} {% endif %}
{% if iface['inet6']['gateway'] is defined %} {% if iface['inet6']['gateway'] is defined %}
gateway6: "{{ iface['inet6']['gateway'] }}" gateway6: {{ iface['inet6']['gateway'] }}
{% endif %} {% endif %}
{% if network_dns_nameservers is defined %} {% if network_dns_nameservers is defined %}
nameservers: nameservers: