servers/roles/alpine/templates/interfaces.j2
Timotej Lazar 7a4a868d41 alpine: add support for VRF interfaces
Mostly so we can merge the firewall role from the network repo, there
aren’t any other current users.
2025-10-22 19:30:31 +02:00

56 lines
1.8 KiB
Django/Jinja

{# Loopback interface must be present so create it here if none is defined in inventory. #}
{% if not interfaces | selectattr("name", "==", "lo") %}
auto lo
iface lo
{% endif -%}
{# Define VRFs. #}
{% for vrf in interfaces | selectattr("vrf") | map(attribute="vrf.name") %}
auto {{ vrf }}
iface {{ vrf }}
pre-up ip link add $IFACE type vrf table {{ 100 + loop.index }}
up ip link set dev $IFACE up
post-down ip link del $IFACE
{% endfor -%}
{# Skip disabled and OOB management interfaces. #}
{# For VMs we have to set the attribute manually (to false) so rejectattr works. #}
{% for iface in interfaces
| defaultattr('mgmt_only')
| rejectattr('mgmt_only')
| selectattr('enabled') %}
auto {{ iface.name }}
iface {{ iface.name }}
{% if iface.vrf %}
requires {{ iface.vrf.name }}
pre-up ip link set $IFACE master {{ iface.vrf.name }}
{% endif %}
{% if iface.mtu %}
mtu {{ iface.mtu }}
{% endif %}
{% for address in iface.ip_addresses %}
address {{ address.address }}
{% if address.family.value == 4 %}
{% set subnet = address.address | ipaddr('subnet') %}
{% set prefix = prefixes | selectattr('prefix', '==', subnet) | first %}
{% set gateway = prefix.custom_fields.gateway.address %}
{% if gateway is defined and gateway != address.address %}
{% if iface.vrf %}
up ip route add default via {{ gateway | ipaddr('address') }} {% if iface.vrf.name %}vrf {{ iface.vrf.name }}{% endif +%}
{% else %}
gateway {{ gateway | ipaddr('address') }}
{% endif %}
{% endif %}
{% endif %}
{% endfor -%}
{# disable SLAAC if we have a manually set IPv6 address #}
{% if iface.ip_addresses | selectattr("family.value", "==", 6) %}
pre-up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/autoconf
{% endif %}
{% endfor -%}
source-directory /etc/network/interfaces.d