Overview

I’m running a dynamic Nameservice for my datacenter authorative for example.com. Now i want to add address records and reverse pointer records to dns to be able to resolv names.

Solution

To achieve this i added variables to my inventory:

cat /etc/ansible/hosts 

[glusterleft]
gluster11 fqdn=gluster11.example.com. ipaddress=172.16.20.103 reverse=103.20.16.172.in-addr.arpa.
gluster12 fqdn=gluster12.example.com. ipaddress=172.16.20.104 reverse=104.20.16.172.in-addr.arpa.

[glusterright]
gluster21 fqdn=gluster21.example.com. ipaddress=172.16.20.203 reverse=203.20.16.172.in-addr.arpa.
gluster22 fqdn=gluster22.example.com. ipaddress=172.16.20.204 reverse=204.20.16.172.in-addr.arpa.

[rhevleft]
rhev11 fqdn=rhev11.example.com. ipaddress=172.16.20.101 reverse=101.20.16.172.in-addr.arpa.
rhev12 fqdn=rhev12.example.com. ipaddress=172.16.20.102 reverse=102.20.16.172.in-addr.arpa.

[rhevright]
rhev21 fqdn=rhev21.example.com. ipaddress=172.16.20.201 reverse=201.20.16.172.in-addr.arpa.
rhev22 fqdn=rhev22.example.com. ipaddress=172.16.20.202 reverse=202.20.16.172.in-addr.arpa.

Note: It would definitely be possible to get the reverse name created out of the ip automatically. – I leave this as an shell exercise for you.

And wrote the following playbook:

[root@jump ansible]# cat named_addhosts.yml
---
- hosts: servers
 gather_facts: False
 serial: 1

tasks:
 - name: check dns
 local_action: shell host {{ fqdn }}
 register: dnsout
 ignore_errors: yes

- name: add dnsentry
 local_action: script /etc/ansible/named_update.sh {{ fqdn }} {{ ipaddress }} {{ reverse }} 
 when: dnsout.stdout.find('{{ ipaddress }}') == -1
 run_once: true
which is supported by a small helper shell script:

[root@jump ansible]# cat /etc/ansible/named_update.sh
#! /usr/bin/bash
# small script which updates dns van nsupdate
# needs 3 parameters
# hostname - full qualified (with a . at the end)
# ipaddress
# reverse - reverse ipaddress full qualified (with in-addr.arpa. ) 
if [ $# -ne 3 ]
then
 echo "usage: $0 hostname ipaddress reverse" >&2
 echo " with:" >&2
 echo " hostname - full qualified (with a . at the end)" >&2
 echo " ipaddress" >&2
 echo " reverse - reverse ipaddress full qualified (with in-addr.arpa. ) " >&2
 exit 1
fi
echo $1
echo $2
echo $3
nsupdate -k /etc/rndc.key << EOF
update add $1 3600 A $2
send
update add $3 3600 PTR $1
send
EOF
 

It is also necessary to grant access to modify the DNS server:. This access is granted through /etc/rndc.key which i put in place before.

Conclusion

I’m now easily able to add Ansible managed hosts into a dns service.