Constant (or macro) definition for a BIND9 zone database

I am using BIND9 on a Linux system. In my zone db files I have multiple occurrences for the same IP in multiple IN A entries. For example, my www, mail, ns, etc., IN A entries refer to the same IP addresses. I do not want and I cannot use IN CNAME entries for this.

For example, part of my DNS zone db file might be something like this:

...
ns1  IN A    _ip1_
     IN AAAA _ip2_
ns2  IN A    _ip3_
     IN AAAA _ip4_
...
wwwa IN A    _ip1_
     IN AAAA _ip2_
...
wwwb IN A    _ip3_
     IN AAAA _ip4_
...
mail IN A    _ip1_
     IN AAAA _ip2_
     IN A    _ip3_
     IN AAAA _ip4_
...
ntp  IN A    _ip1_
     IN AAAA _ip2_
     IN A    _ip3_
     IN AAAA _ip4_
...

In the above example, on each place where I have to enter _ip1_ for example, I have to enter the a same IP address. This is prone to errors.

How can I define a "macro" or a "constant" and use it later in a BIND9 db file?


Solution 1:

You can't define variables or constants in a Bind9 zone file. Bind9 only supports $ORIGIN, $TTL, $GENERATE and $INCLUDE as "programmatic" entries, neither of which do what you need.

It would be fairly trivial to use a template-like system. You could use a bash script with a heredoc, or use the envsubst utility:

  1. Create a "template" zone file:
ns1  IN A    $ip1
     IN AAAA $ip2
ns2  IN A    $ip3
     IN AAAA $ip4
...
wwwa IN A    $ip1
     IN AAAA $ip2
  1. Write a small shell script that sets the variables and does the expansion, for example:
#!/bin/bash

ip1='198.51.100.42'
ip2='2001:db8:1234:100::42'
ip3='198.51.100.17'
ip4='2001:db8:1234:100::17'

envsubst < template_file > real_zone_file.zone