From 71d1da1cf12586410da11f8212d73f66011ab028 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Tue, 30 Aug 2022 23:27:03 -0500 Subject: [PATCH] Simply network role The role only supports netplan now --- roles/network/defaults/main.yml | 14 ----- roles/network/handlers/main.yml | 21 -------- roles/network/tasks/Ubuntu.yaml | 6 +++ roles/network/tasks/ifupdown.yml | 22 -------- roles/network/tasks/main.yml | 52 ------------------- roles/network/tasks/netplan.yml | 18 +++---- roles/network/tasks/systemd-networkd.yml | 33 ------------ roles/network/templates/netcfg.yaml.j2 | 47 ----------------- roles/network/templates/network-interfaces.j2 | 8 --- roles/network/templates/resolv.conf.j2 | 15 ------ roles/network/templates/resolved.conf.j2 | 13 ----- roles/network/templates/systemd-network.j2 | 41 --------------- .../vars/{Ubuntu-18.yaml => Ubuntu.yaml} | 0 roles/network/vars/main.yml | 2 - roles/network/vars/netplan.yaml | 15 ++++++ 15 files changed, 27 insertions(+), 280 deletions(-) create mode 100644 roles/network/tasks/Ubuntu.yaml delete mode 100644 roles/network/tasks/ifupdown.yml delete mode 100644 roles/network/tasks/systemd-networkd.yml delete mode 100644 roles/network/templates/netcfg.yaml.j2 delete mode 100644 roles/network/templates/network-interfaces.j2 delete mode 100644 roles/network/templates/resolv.conf.j2 delete mode 100644 roles/network/templates/resolved.conf.j2 delete mode 100644 roles/network/templates/systemd-network.j2 rename roles/network/vars/{Ubuntu-18.yaml => Ubuntu.yaml} (100%) delete mode 100644 roles/network/vars/main.yml create mode 100644 roles/network/vars/netplan.yaml diff --git a/roles/network/defaults/main.yml b/roles/network/defaults/main.yml index c8668a8..ed97d53 100644 --- a/roles/network/defaults/main.yml +++ b/roles/network/defaults/main.yml @@ -1,15 +1 @@ --- -network_nameservers: [] -network_resolv_conf_options: - - edns0 - -_network_resolv_conf_enabled: yes - -_network_systemd_networkd_service_name: systemd-networkd.service -_network_ifupdown_service_name: networking.service -_network_systemd_resolved_service_name: systemd-resolved.service - -_network_ifupdown_package_name: ifupdown -_network_netplan_package_name: netplan.io - -# vim:ft=yaml.ansible: diff --git a/roles/network/handlers/main.yml b/roles/network/handlers/main.yml index ff21e93..2ecbab7 100644 --- a/roles/network/handlers/main.yml +++ b/roles/network/handlers/main.yml @@ -1,24 +1,3 @@ --- -- name: restart systemd-networkd - service: - name: "{{ _network_systemd_networkd_service_name }}" - state: restarted - when: _network_systemd_networkd_enabled - -- name: restart ifupdown - service: - name: "{{ _network_ifupdown_service_name }}" - state: restarted - when: _network_ifupdown_enabled - - name: netplan apply command: netplan apply - when: _network_netplan_enabled - -- name: restart systemd-resolved - service: - name: "{{ _network_systemd_resolved_service_name }}" - state: restarted - when: _network_systemd_resolved_available - -# vim:ft=yaml.ansible: diff --git a/roles/network/tasks/Ubuntu.yaml b/roles/network/tasks/Ubuntu.yaml new file mode 100644 index 0000000..430a526 --- /dev/null +++ b/roles/network/tasks/Ubuntu.yaml @@ -0,0 +1,6 @@ +--- +- name: Include netplan vars + include_vars: netplan.yaml + +- name: Include netplan tasks + include_tasks: netplan.yml diff --git a/roles/network/tasks/ifupdown.yml b/roles/network/tasks/ifupdown.yml deleted file mode 100644 index bb1845b..0000000 --- a/roles/network/tasks/ifupdown.yml +++ /dev/null @@ -1,22 +0,0 @@ ---- -# ifupdown - -- name: manage ifupdown services - service: - name: "{{ _network_ifupdown_service_name }}" - state: "{{ _network_ifupdown_enabled | ternary('started', 'stopped') }}" - enabled: "{{ _network_ifupdown_enabled }}" - -- name: manage ifupdown package - package: - name: "{{ _network_ifupdown_package_name }}" - state: "{{ _network_ifupdown_enabled | ternary('present', 'absent') }}" - -- name: manage /etc/network/interfaces - template: - src: network-interfaces.j2 - dest: /etc/network/interfaces - notify: restart ifupdown - with_items: "{{ network_interfaces }}" - when: _network_ifupdown_enabled | bool - diff --git a/roles/network/tasks/main.yml b/roles/network/tasks/main.yml index 4ec1a4c..64d3b46 100644 --- a/roles/network/tasks/main.yml +++ b/roles/network/tasks/main.yml @@ -22,55 +22,3 @@ - "default.yaml" paths: - tasks - -- 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 - -- name: configure resolv.conf - template: - src: resolv.conf.j2 - dest: /etc/resolv.conf - notify: restart systemd-resolved - when: _network_resolv_conf_enabled | bool - -# systemd-resolved - -- name: manage systemd-resolved service - service: - name: "{{ _network_systemd_resolved_service_name }}" - state: "{{ _network_systemd_resolved_enabled | ternary('started', 'stopped') }}" - enabled: "{{ _network_systemd_resolved_enabled }}" - -- name: symlink /etc/resolv.conf - file: - src: /run/systemd/resolve/stub-resolv.conf - dest: /etc/resolv.conf - state: link - force: yes - when: _network_systemd_resolved_enabled | bool - -- name: configure systemd-resolved - template: - src: resolved.conf.j2 - dest: /etc/systemd/resolved.conf - notify: restart systemd-resolved - when: _network_systemd_resolved_enabled | bool - -- name: remove systemd-resolved configuration - file: - path: /etc/systemd/resolved.conf - state: absent - when: not _network_systemd_resolved_enabled | bool - -# vim:ft=yaml.ansible: diff --git a/roles/network/tasks/netplan.yml b/roles/network/tasks/netplan.yml index 3915e61..a9a228a 100644 --- a/roles/network/tasks/netplan.yml +++ b/roles/network/tasks/netplan.yml @@ -1,15 +1,9 @@ --- -# netplan.io - -- name: manage netplan package - package: - name: "{{ _network_netplan_package_name }}" - state: "{{ _network_netplan_enabled | ternary('present', 'absent') }}" - -- name: manage netplan - template: - src: netcfg.yaml.j2 +- name: Configure netplan + copy: dest: /etc/netplan/01-netcfg.yaml + owner: root + group: root + mode: '0644' + content: "{{ network_netplan_config | to_nice_yaml }}" notify: netplan apply - when: _network_netplan_enabled | bool - diff --git a/roles/network/tasks/systemd-networkd.yml b/roles/network/tasks/systemd-networkd.yml deleted file mode 100644 index 48e309e..0000000 --- a/roles/network/tasks/systemd-networkd.yml +++ /dev/null @@ -1,33 +0,0 @@ ---- -# systemd-networkd - -- name: manage systemd-networkd package - package: - name: "{{ _network_systemd_networkd_package_name }}" - state: "{{ _network_systemd_networkd_enabled | ternary('present', 'absent') }}" - when: _systemd_networkd_package_name is defined - -- name: manage systemd-networkd service - service: - name: "{{ _network_systemd_networkd_service_name }}" - state: "{{ _network_systemd_networkd_enabled | ternary('started', 'stopped') }}" - enabled: "{{ _network_systemd_networkd_enabled }}" - -- name: configure systemd-networkd - template: - src: systemd-network.j2 - dest: "/etc/systemd/network/05-{{ item.name }}.network" - notify: restart systemd-networkd - with_items: "{{ network_interfaces }}" - 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 deleted file mode 100644 index 6424031..0000000 --- a/roles/network/templates/netcfg.yaml.j2 +++ /dev/null @@ -1,47 +0,0 @@ -# {{ 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) | ansible.utils.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/network-interfaces.j2 b/roles/network/templates/network-interfaces.j2 deleted file mode 100644 index 6741a09..0000000 --- a/roles/network/templates/network-interfaces.j2 +++ /dev/null @@ -1,8 +0,0 @@ -# {{ ansible_managed }} - -{% if _network_ifupdown_enabled %} -{% elif _network_netplan_enabled %} -# netplan is in use. -{% elif _network_systemd_networkd_enabled %} -# systemd-networkd is in use. -{% endif %} diff --git a/roles/network/templates/resolv.conf.j2 b/roles/network/templates/resolv.conf.j2 deleted file mode 100644 index ca0d186..0000000 --- a/roles/network/templates/resolv.conf.j2 +++ /dev/null @@ -1,15 +0,0 @@ -# {{ ansible_managed }} - -{% for ns in (network_nameservers | shuffle(seed=inventory_hostname))[:3] %} -nameserver {{ ns }} -{% endfor %} -{% if network_resolv_conf_options is defined %} -options {{ network_resolv_conf_options | join(' ') }} -{% endif %} -{% if network_search is defined %} -{% if network_search is iterable and network_search is not string %} -search {{ network_search | join(' ') }} -{% else %} -search {{ network_search }} -{% endif %} -{% endif %} diff --git a/roles/network/templates/resolved.conf.j2 b/roles/network/templates/resolved.conf.j2 deleted file mode 100644 index 475cb3c..0000000 --- a/roles/network/templates/resolved.conf.j2 +++ /dev/null @@ -1,13 +0,0 @@ -# {{ ansible_managed }} - -[Resolve] -{% for ns in (network_nameservers | shuffle(seed=inventory_hostname)) %} -DNS={{ ns }} -{% endfor %} -{% if network_search is defined %} -{% if network_search is iterable and network_search is not string %} -Domains={{ network_search | join(' ') }} -{% else %} -Domains={{ network_search }} -{% endif %} -{% endif %} diff --git a/roles/network/templates/systemd-network.j2 b/roles/network/templates/systemd-network.j2 deleted file mode 100644 index b1630a1..0000000 --- a/roles/network/templates/systemd-network.j2 +++ /dev/null @@ -1,41 +0,0 @@ -# {{ ansible_managed }} - -{% if item is defined %} -{% if item.name is defined %} -[Match] -Name={{ item.name }} -{% endif %} -{% if item.address is defined %} -[Network] -{% for addr in item.address %} -Address={{ addr }} -{% endfor %} -{% endif %} -{% 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) | ansible.utils.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.yaml similarity index 100% rename from roles/network/vars/Ubuntu-18.yaml rename to roles/network/vars/Ubuntu.yaml diff --git a/roles/network/vars/main.yml b/roles/network/vars/main.yml deleted file mode 100644 index b4706aa..0000000 --- a/roles/network/vars/main.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -# vars file for roles/network \ No newline at end of file diff --git a/roles/network/vars/netplan.yaml b/roles/network/vars/netplan.yaml new file mode 100644 index 0000000..081b47c --- /dev/null +++ b/roles/network/vars/netplan.yaml @@ -0,0 +1,15 @@ +network_netplan_config: + network: + version: 2 + renderer: networkd + ethernets: + eth0: + dhcp4: 'no' + dhcp6: 'no' + 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 }}" + gateway4: "{{ ansible_default_ipv4.gateway }}" + gateway6: "{{ ansible_default_ipv6.gateway }}" + nameservers: + addresses: "{{ dns_servers }}"