Simply network role

The role only supports netplan now
This commit is contained in:
Ryan Cavicchioni 2022-08-30 23:27:03 -05:00
parent 221578a0d1
commit 71d1da1cf1
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
15 changed files with 27 additions and 280 deletions

View File

@ -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:

View File

@ -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:

View File

@ -0,0 +1,6 @@
---
- name: Include netplan vars
include_vars: netplan.yaml
- name: Include netplan tasks
include_tasks: netplan.yml

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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 %}

View File

@ -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 %}

View File

@ -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 %}

View File

@ -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 %}

View File

@ -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 %}

View File

@ -1,2 +0,0 @@
---
# vars file for roles/network

View File

@ -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 }}"