Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We have a standard high-availability setup at a co-location facility that we host client programs on. For some programs, we host the DNS, but I find that setting up multiple zones manually can be tedious and prone to error. Is there an easy way to setup a standard zone file template and some how run sed, awk, or some other program to process a set of variables from another file and merge them with the template to create a zone file?

I thought about using m4, but I'm not well versed and it only seems ideal for merging one template at a time.

Ideally, I'd like to take a standard zone file template and merge it with a list like so:

domain1=ip1
domain2=ip2
domain3=ip1
domain4=ip1
etc...

Is there a way to script this?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

I think m4 would be overkill. My first instinct would be bash + sed, but if you have any familiarity with a higher-level language that might be easier. Off the top of my head, I would imagine something like this:

Data file:

example.com:192.168.10.1
fake.com:192.168.10.2
foo.com:192.168.10.3
bar.com:192.168.10.4

Script:

#!/bin/bash

DATAFILE=datafile
TEMPLATE=template.txt

for data in $(cat $DATAFILE)
do
    dom=${data%:*}
    ip=${data#*:}
    sed "{ s/DOMAIN/$dom/;
           s/IPADDR/$ip/;
         }" $TEMPLATE > $dom.zone
done

And your template zone file would have "DOMAIN" or "IPADDR" where appropriate.

This is completely untested and should be refined before use.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.