From 317bed2eb62c34e3b54fb5eef824fb454f4dcd37 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 5 Oct 2019 16:43:05 -0500 Subject: [PATCH] Refactor network role --- roles/network/tasks/main.yml | 3 ++ roles/network/tasks/systemd-networkd.yml | 12 +++++- roles/network/templates/netcfg.yaml.j2 | 47 ++++++++++++++++++++++ roles/network/templates/systemd-network.j2 | 45 +++++++++++++++------ roles/network/vars/Ubuntu-18.yaml | 4 ++ 5 files changed, 98 insertions(+), 13 deletions(-) diff --git a/roles/network/tasks/main.yml b/roles/network/tasks/main.yml index aaa7768..65fef25 100644 --- a/roles/network/tasks/main.yml +++ b/roles/network/tasks/main.yml @@ -8,12 +8,15 @@ - name: systemd-networkd include: systemd-networkd.yml + when: network_manage_systemd_networkd - name: ifupdown include: ifupdown.yml + when: network_manage_ifupdown - name: netplan.io include: netplan.yml + when: network_manage_netplan # resolv.conf diff --git a/roles/network/tasks/systemd-networkd.yml b/roles/network/tasks/systemd-networkd.yml index 962c231..48e309e 100644 --- a/roles/network/tasks/systemd-networkd.yml +++ b/roles/network/tasks/systemd-networkd.yml @@ -19,5 +19,15 @@ dest: "/etc/systemd/network/05-{{ item.name }}.network" notify: restart systemd-networkd with_items: "{{ network_interfaces }}" - when: _network_systemd_networkd_enabled | bool + when: + - network_interfaces is defined + - _network_systemd_networkd_enabled | bool +- name: configure systemd-networkd default interface + template: + src: systemd-network.j2 + dest: "/etc/systemd/network/05-{{ ansible_default_ipv4.interface }}.network" + notify: restart systemd-networkd + when: + - network_interfaces is not defined + - _network_systemd_networkd_enabled | bool diff --git a/roles/network/templates/netcfg.yaml.j2 b/roles/network/templates/netcfg.yaml.j2 index e69de29..afdad76 100644 --- a/roles/network/templates/netcfg.yaml.j2 +++ b/roles/network/templates/netcfg.yaml.j2 @@ -0,0 +1,47 @@ +# {{ ansible_managed }} + +{% if network_interfaces is defined %} +network: + version: 2 + ethernets: +{% for iface in network_interfaces %} +{% if iface.name is defined %} + {{ iface.name }}: +{% endif %} +{% if iface.address is defined %} + addresses: +{% for addr in iface.address %} + - {{ addr }} +{% endfor %} +{% endif %} +{% if iface.gateway4 is defined %} + gateway4: {{ iface.gateway4 }} +{% endif %} +{% if iface.gateway6 is defined %} + gateway6: {{ iface.gateway6 }} +{% endif %} +{% endfor %} +{% else %} +{# Default network configuration #} +network: + version: 2 + ethernets: +{% if ansible_default_ipv4.interface is defined %} + {{ ansible_default_ipv4.interface }}: +{% if ansible_default_ipv4.address is defined or ansible_default_ipv6.address is defined %} + addresses: +{% if ansible_default_ipv4.address is defined %} + - {{ ansible_default_ipv4.address }}/{{ (ansible_default_ipv4.network + "/" + ansible_default_ipv4.netmask) | ipaddr('prefix') }} +{% endif %} +{% if ansible_default_ipv6.address is defined %} + - {{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }} +{% endif %} +{% if ansible_default_ipv4.gateway is defined %} + gateway4: {{ ansible_default_ipv4.gateway }} +{% endif %} +{% if ansible_default_ipv6.gateway is defined %} + gateway6: {{ ansible_default_ipv6.gateway }} +{% endif %} +{% endif %} +{% endif %} +{% endif %} diff --git a/roles/network/templates/systemd-network.j2 b/roles/network/templates/systemd-network.j2 index 7dfcb26..b29883b 100644 --- a/roles/network/templates/systemd-network.j2 +++ b/roles/network/templates/systemd-network.j2 @@ -1,20 +1,41 @@ # {{ ansible_managed }} +{% if item is defined %} +{% if item.name is defined %} [Match] Name={{ item.name }} - +{% endif %} +{% if item.address is defined %} [Network] -{% if item.address is string %} -Address={{ item.address }} -{% else %} -{% for address in item.address %} -Address={{ address }} +{% for addr in item.address %} +Address={{ addr }} {% endfor %} {% endif %} -{% if item.gateway is string %} -Gateway={{ item.gateway }} -{% else %} -{% for gateway in item.gateway %} -Gateway={{ gateway }} -{% endfor %} +{% if item.gateway4 is defined %} +Gateway={{ item.gateway4 }} +{% endif %} +{% if item.gateway6 is defined %} +Gateway={{ item.gateway6 }} +{% endif %} +{% else %} +{# Default network configuration #} +{% if ansible_default_ipv4.interface is defined %} +[Match] +Name={{ ansible_default_ipv4.interface }} +{% if ansible_default_ipv4.address is defined or ansible_default_ipv6.address is defined %} +[Network] +{% if ansible_default_ipv4.address is defined %} +Address={{ ansible_default_ipv4.address }}/{{ (ansible_default_ipv4.network + "/" + ansible_default_ipv4.netmask) | ipaddr('prefix') }} +{% endif %} +{% if ansible_default_ipv6.address is defined %} +Address={{ ansible_default_ipv6.address }}/{{ ansible_default_ipv6.prefix }} +{% endif %} +{% if ansible_default_ipv4.gateway is defined %} +Gateway={{ ansible_default_ipv4.gateway }} +{% endif %} +{% if ansible_default_ipv6.gateway is defined %} +Gateway={{ ansible_default_ipv6.gateway }} +{% endif %} +{% endif %} +{% endif %} {% endif %} diff --git a/roles/network/vars/Ubuntu-18.yaml b/roles/network/vars/Ubuntu-18.yaml index c27dd29..99001b9 100644 --- a/roles/network/vars/Ubuntu-18.yaml +++ b/roles/network/vars/Ubuntu-18.yaml @@ -8,4 +8,8 @@ _network_systemd_networkd_enabled: yes _network_systemd_resolved_enabled: no _network_resolv_conf_enabled: yes +network_manage_ifupdown: no +network_manage_systemd_networkd: yes +network_manage_netplan: no + # vim:ft=yaml.ansible: