From b76a61acbd76fc3ad6c1423bffe1d460fcfc7fa5 Mon Sep 17 00:00:00 2001 From: David Galloway Date: Mon, 8 Dec 2025 19:53:19 -0500 Subject: [PATCH] nameserver: Support multiple ipvars Signed-off-by: David Galloway --- roles/nameserver/README.rst | 8 ++++--- roles/nameserver/templates/forward.j2 | 26 ++++++++++++++-------- roles/nameserver/templates/reverse.j2 | 31 +++++++++++++++++++-------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/roles/nameserver/README.rst b/roles/nameserver/README.rst index 85ac3d37..56209b57 100644 --- a/roles/nameserver/README.rst +++ b/roles/nameserver/README.rst @@ -103,8 +103,8 @@ The ``named_domains`` dictionary is the bread and butter of creating zone files. ``forward`` The domain of the forward lookup zone for each domain (key) -``ipvar`` - The variable assigned to a system in the Ansible inventory. This allows systems to have multiple IPs assigned for a front and ipmi network, for example. See **Inventory Example** below. +``ipvar`` OR ``ipvars[]`` + The variable(s) assigned to a system in the Ansible inventory. This allows systems to have multiple IPs assigned for a front and ipmi network, for example. See **Inventory Example** below. ``dynamic`` Specifies whether the parent zone/domain should allow Dynamic DNS records. See **Dynamic DNS** below for more information. @@ -130,7 +130,9 @@ The ``named_domains`` dictionary is the bread and butter of creating zone files. - www IN TXT "my www host" - ns1.private IN A 192.168.0.1 private.example.com: - ipvar: ip + ipvars: + - ip + - if_25Gb_ip dynamic: true ddns_hostname_prefixes: - dyn diff --git a/roles/nameserver/templates/forward.j2 b/roles/nameserver/templates/forward.j2 index 5ce8c2e7..5f67cdee 100644 --- a/roles/nameserver/templates/forward.j2 +++ b/roles/nameserver/templates/forward.j2 @@ -1,7 +1,13 @@ {% set domain = item.key %} -{% if item.value.ipvar is defined and item.value.ipvar.0 is defined %} -{% set ipvar = item.value.ipvar %} + +{# Normalize ipvars: prefer item.value.ipvars (list), fallback to single ipvar #} +{% set ipvars = [] %} +{% if item.value.ipvars is defined %} + {% set ipvars = item.value.ipvars %} +{% elif item.value.ipvar is defined %} + {% set ipvars = [ item.value.ipvar ] %} {% endif %} + ; ; {{ ansible_managed }} ; @@ -26,11 +32,13 @@ $ORIGIN {{ domain }}. {% endfor %} {% endif %} -{% if item.value.ipvar is defined and item.value.ipvar.0 is defined %} -{% for host in groups['all'] %} -{% if hostvars[host][ipvar] is defined %} -{% set ipaddr = hostvars[host][ipvar] %} -{{ hostvars[host]['inventory_hostname_short'] }} IN A {{ hostvars[host][ipvar] }} -{% endif %} -{% endfor %} +{# Emit A records for each host and each ipvar #} +{% if ipvars | length > 0 %} +{% for host in groups['all'] %} +{% for ipvar in ipvars %} +{% if hostvars[host][ipvar] is defined %} +{{ hostvars[host]['inventory_hostname_short'] }} IN A {{ hostvars[host][ipvar] }} +{% endif %} +{% endfor %} +{% endfor %} {% endif %} diff --git a/roles/nameserver/templates/reverse.j2 b/roles/nameserver/templates/reverse.j2 index 6d6e82a0..3f76e581 100644 --- a/roles/nameserver/templates/reverse.j2 +++ b/roles/nameserver/templates/reverse.j2 @@ -1,6 +1,14 @@ {% set zone = item.1 %} {% set domain = item.0.forward %} -{% set ipvar = item.0.ipvar %} + +{# Normalize ipvars from item.0: prefer ipvars list, fall back to ipvar #} +{% set ipvars = [] %} +{% if item.0.ipvars is defined and item.0.ipvars %} + {% set ipvars = item.0.ipvars %} +{% elif item.0.ipvar is defined and item.0.ipvar %} + {% set ipvars = [ item.0.ipvar ] %} +{% endif %} + ; ; {{ ansible_managed }} ; @@ -19,12 +27,17 @@ $TTL {{ named_conf_soa_ttl }} ; Reverse zone {{ zone }} belongs to forward zone {{ domain }} -{% for host in groups['all'] %} -{% if hostvars[host][ipvar] is defined %} -{% set octet1,octet2,octet3,octet4 = hostvars[host][ipvar].split('.') %} -{% set cutip = octet1 + '.' + octet2 + '.' + octet3 %} -{% if cutip == zone %} -{{ octet4 }} IN PTR {{ hostvars[host]['inventory_hostname_short'] }}.{{ domain }}. +{% if ipvars | length > 0 %} +{% for host in groups['all'] %} +{% for ipvar in ipvars %} +{% if hostvars[host][ipvar] is defined %} +{% set octet1,octet2,octet3,octet4 = hostvars[host][ipvar].split('.') %} +{% set cutip = octet1 + '.' + octet2 + '.' + octet3 %} +{% if cutip == zone %} +{% set short = hostvars[host].inventory_hostname_short | default(host) %} +{{ octet4 }} IN PTR {{ short }}.{{ domain }}. +{% endif %} +{% endif %} +{% endfor %} +{% endfor %} {% endif %} -{% endif %} -{% endfor %} -- 2.47.3