Allow configuring network interfaces

This commit is contained in:
Albert Stefanov 2024-05-04 20:16:12 +03:00
parent d28d667b8d
commit f3b2b68b64
8 changed files with 90 additions and 31 deletions

View File

@ -5,4 +5,4 @@ global_packages:
- mtr-tiny - mtr-tiny
- traceroute - traceroute
- tcpdump - tcpdump
- ethtool - ethtool

View File

@ -0,0 +1,24 @@
---
interfaces:
xgei0.100:
ipv4:
method: static
address: 192.0.2.2/24
nameservers:
- 8.8.8.8
- 8.8.4.4
gateway: 192.0.2.1
metric: 500
ipv6:
method: static
address: 2001:db8::2/64
nameservers:
- 2001:4860:4860::8888
- 2001:4860:4860::8844
gateway: 2001:db8::1
metric: 600
xgei0.101:
ipv4:
method: manual
ipv6:
method: auto

View File

@ -1 +1 @@
ansible-lint roles/*/tasks/main.yml ansible-lint roles/*/tasks/main.yml host_vars/**/*.yml group_vars/**/*.yml roles/*/handlers/main.yml

View File

@ -1,7 +1,14 @@
--- ---
- name: update initramfs - name: Restart networking
ansible.builtin.service:
name: networking
state: restarted
- name: Update initramfs
ansible.builtin.command: "update-initramfs -u" ansible.builtin.command: "update-initramfs -u"
changed_when: true
# register: restart_required # register: restart_required
- name: trigger udev add - name: Trigger udev add
ansible.builtin.command: "udevadm trigger --action=add" ansible.builtin.command: "udevadm trigger --action=add"
changed_when: true

View File

@ -1,27 +1,10 @@
--- ---
- name: Check whether vlan config is valid - name: Create interface files
ansible.builtin.assert: ansible.builtin.template:
that: src: interface.conf.j2
- item.value.name is defined if item.value.type == "eth"
- item.value.parent is defined if item.value.type == "vlan"
- item.value.parent in interfaces if item.value.parent is defined
with_items:
- interfaces
- name: Create physical interfaces
community.general.interfaces_file:
dest: "/etc/network/interfaces.d/{{ item.key }}.conf" dest: "/etc/network/interfaces.d/{{ item.key }}.conf"
iface: "{{ item.value.name }}" mode: "644"
with_items: with_items:
- interfaces | select(item.value.type != "vlan") - "{{ interfaces | dict2items }}"
notify:
- name: Create vlan interfaces - Restart networking
community.general.interfaces_file:
dest: "/etc/network/interfaces.d/{{ item.key }}.conf"
iface: "{{ interfaces[item.value.parent].name }}.{{ item.value.vlan_id }}"
with_items:
- interfaces | select(item.value.type == "vlan")
- name: Restart networking
ansible.builtin.notify:
- restart networking

View File

@ -30,3 +30,7 @@
- name: Configure udev rules - name: Configure udev rules
ansible.builtin.include_tasks: udev.yml ansible.builtin.include_tasks: udev.yml
when: udev_rules is defined when: udev_rules is defined
- name: Configure network interfaces
ansible.builtin.include_tasks: interfaces.yml
when: interfaces is defined

View File

@ -13,5 +13,5 @@
with_items: with_items:
- "{{ udev_rules | dict2items }}" - "{{ udev_rules | dict2items }}"
notify: notify:
- update initramfs - Update initramfs
- trigger udev add - Trigger udev add

View File

@ -0,0 +1,41 @@
{% if not(item.shutdown is defined) or not(item.shutdown) %}
auto {{ item.key }}
{% endif %}
{% if 'ipv4' in item.value %}
{%- set props = item.value.ipv4 -%}
iface {{ item.key }} inet {{ props.method }}
{% if props.method == 'static' %}
address {{ props.address }}
{% if 'nameservers' in props -%}
dns-nameservers {{ props.nameservers | join(' ') }}
{% endif -%}
{% if 'gateway' in props -%}
{% if not(props.metric is defined) -%}
gateway {{ props.gateway }}
{% else -%}
post-up /sbin/ip route add default via {{ props.gateway }} dev {{ item.key }} metric {{ props.metric }}
post-down /sbin/ip route del default via {{ props.gateway }} dev {{ item.key }} metric {{ props.metric }}
{% endif -%}
{% endif -%}
{%- endif %}
{% endif %}
{% if 'ipv6' in item.value -%}
{%- set props = item.value.ipv6 -%}
iface {{ item.key }} inet6 {{ props.method }}
{% if props.method == 'static' %}
address {{ props.address }}
{% if 'nameservers' in props -%}
dns-nameservers {{ props.nameservers | join(' ') }}
{% endif -%}
{% if 'gateway' in props -%}
{% if not(props.metric is defined) -%}
gateway {{ props.gateway }}
{% else -%}
post-up /sbin/ip -6 route add default via {{ props.gateway }} dev {{ item.key }} metric {{ props.metric }}
post-down /sbin/ip -6 route del default via {{ props.gateway }} dev {{ item.key }} metric {{ props.metric }}
{% endif -%}
{% endif -%}
{%- endif %}
{% endif %}