From 0ed4973894ceee0884e0d9d1af527900c3aed7f9 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Mon, 24 Mar 2025 18:13:55 +0100 Subject: [PATCH] access: get mgmt gateway from custom field on prefix Mainly so we can drop the gateway custom field from NetBox interfaces. --- roles/access/templates/config-d-link.j2 | 6 ++++-- roles/access/templates/config-fs.j2 | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/roles/access/templates/config-d-link.j2 b/roles/access/templates/config-d-link.j2 index 3250be7..e0b356f 100644 --- a/roles/access/templates/config-d-link.j2 +++ b/roles/access/templates/config-d-link.j2 @@ -68,8 +68,10 @@ interface {{ iface.name }} {# store management address and gateway for later #} {%- if iface.mgmt_only and iface.ip_addresses %} {% set mgmt.ip = iface.ip_addresses[0].address %} -{% if iface.custom_fields.gateway %} -{% set mgmt.gw = iface.custom_fields.gateway.address %} +{% set subnet = mgmt.ip | ipaddr('subnet') %} +{% set prefix = prefixes | selectattr('prefix', '==', subnet) | first %} +{% if prefix.custom_fields.gateway %} +{% set mgmt.gw = prefix.custom_fields.gateway.address %} {% endif %} {% endif %} diff --git a/roles/access/templates/config-fs.j2 b/roles/access/templates/config-fs.j2 index d21bfe2..6715715 100644 --- a/roles/access/templates/config-fs.j2 +++ b/roles/access/templates/config-fs.j2 @@ -18,8 +18,10 @@ interface {{ iface.name }} {% if iface.ip_addresses %} {% set address = iface.ip_addresses | map(attribute='address') | first %} ip address {{ address | ipaddr('address') }} {{ address | ipaddr('netmask') }} -{% if iface.custom_fields.gateway %} - gateway {{ iface.custom_fields.gateway.address | ipaddr('address') }} +{% set subnet = address | ipaddr('subnet') %} +{% set prefix = prefixes | selectattr('prefix', '==', subnet) | first %} +{% if prefix.custom_fields.gateway %} + gateway {{ prefix.custom_fields.gateway.address | ipaddr('address') }} {%- endif %} {%- endif %}