fabric: clean up switch.intf template

Add some comments, simplify some logic.
This commit is contained in:
Timotej Lazar 2025-03-26 22:43:26 +01:00
parent 2f662373e5
commit a2d7174829

View file

@ -1,34 +1,42 @@
{# Virtual gateway IPs for MLAG L2 networks are stored as FHRP groups. #}
{% set fhrp_assignments = query('netbox.netbox.nb_lookup', 'fhrp-group-assignments', raw_data=true) %} {% set fhrp_assignments = query('netbox.netbox.nb_lookup', 'fhrp-group-assignments', raw_data=true) %}
{% set fhrp_groups = query('netbox.netbox.nb_lookup', 'fhrp-groups', raw_data=true) -%} {% set fhrp_groups = query('netbox.netbox.nb_lookup', 'fhrp-groups', raw_data=true) -%}
{% for iface in interfaces | rejectattr('name', 'in', ('lo', 'bridge')) | rejectattr('mgmt_only') | selectattr('enabled') %} {% for iface in interfaces
| selectattr('enabled')
| rejectattr('name', 'in', ('lo', 'bridge'))
| rejectattr('mgmt_only') %}
auto {{ iface.name }} auto {{ iface.name }}
iface {{ iface.name }} iface {{ iface.name }}
{# Set VRF and MTU if given. #}
{% if iface.vrf %} {% if iface.vrf %}
vrf {{ iface.vrf.name }} vrf {{ iface.vrf.name }}
{% endif -%} {% endif %}
{% if iface.mtu %} {% if iface.mtu %}
mtu {{ iface.mtu }} mtu {{ iface.mtu }}
{% endif -%} {% endif %}
{% if iface.type.value == 'lag' %}
{#- Bond/MLAG stuff. #} {#- Bond/MLAG stuff. #}
{% set members = interfaces | selectattr('lag') | selectattr('lag.name', '==', iface.name) %} {% if iface.type.value == 'lag' %}
{% set members = interfaces
| selectattr('lag')
| selectattr('lag.name', '==', iface.name) %}
{% if members %} {% if members %}
bond-slaves {{ members | map(attribute='name') | join(' ') }} bond-slaves {{ members | map(attribute='name') | join(' ') }}
{% endif %} {% endif %}
{% if iface.custom_fields.bond_mode %} {% if iface.custom_fields.bond_mode %}
bond-mode {{ iface.custom_fields.bond_mode }} bond-mode {{ iface.custom_fields.bond_mode }}
{% endif %} {% endif %}
{# If the peer shares a bond with the same name, generate a clag-id for it unless the bonded link is to peer itself. #}
{% if peer is defined %} {#- If the peer has a bond with the same name, generate and set a clag-id for the bond. #}
{% set peer_members = hostvars[peer].interfaces | selectattr('lag') | selectattr('lag.name', '==', iface.name) %} {# Don’t set a clag-id for the peerlink (i.e. bond where a member connects to the peer switch). #}
{% if peer_members | iface_peer | reject('eq', inventory_hostname) %} {% if peer is defined and members | iface_peer | reject('eq', peer)%}
{% set peer_members = hostvars[peer].interfaces
| selectattr('lag')
| selectattr('lag.name', '==', iface.name) %}
clag-id {{ (members + peer_members) | cl_clag_id }} clag-id {{ (members + peer_members) | cl_clag_id }}
{% endif %} {% endif %}
{% endif %} {% endif %}
{% endif %}
{#- Set allowed VLANs if we are part of the bridge. #} {#- Set allowed VLANs if we are part of the bridge. #}
{% if iface.bridge and iface.mode %} {% if iface.bridge and iface.mode %}
@ -43,14 +51,21 @@ iface {{ iface.name }}
{% for addr in iface.ip_addresses %} {% for addr in iface.ip_addresses %}
address {{ addr.address }} address {{ addr.address }}
{% endfor %} {% endfor %}
{#- MLAG virtual address. #}
{% if iface.count_fhrp_groups > 0 %} {% if iface.count_fhrp_groups > 0 %}
{% set fhrp_assignment = fhrp_assignments | selectattr('interface.id', '==', iface.id) | first %} {% set fhrp_assignment = fhrp_assignments | selectattr('interface.id', '==', iface.id) | first %}
{% set fhrp_group = fhrp_groups | selectattr('id', '==', fhrp_assignment.group.id) | first %} {% set fhrp_group = fhrp_groups | selectattr('id', '==', fhrp_assignment.group.id) | first %}
address-virtual 00:00:5e:00:01:01 {{ fhrp_group.ip_addresses | sort(attribute='family.value') | map(attribute='address') | join(' ') }} address-virtual 00:00:5e:00:01:01 {{
{% if iface.name in ifaces_directed_broadcast|default([]) %} fhrp_group.ip_addresses
{# Enable directed broadcast forwarding from this interface for WoL. #} | sort(attribute='family.value')
| map(attribute='address')
| join(' ') }}
{#- Enable directed broadcast forwarding on this interface for WoL if requested. +#}
{% if iface.name in ifaces_directed_broadcast | default([]) %}
post-up echo 1 > /proc/sys/net/ipv4/conf/{{ iface.name | replace('.', '-') }}-v0/bc_forwarding post-up echo 1 > /proc/sys/net/ipv4/conf/{{ iface.name | replace('.', '-') }}-v0/bc_forwarding
{% endif %} {% endif %}
{% endif %} {% endif %}
{% endfor %} {%+ endfor %}