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.


To achieve this i added variables to my inventory:

cat /etc/ansible/hosts 

gluster11 fqdn=gluster11.example.com. ipaddress= reverse=
gluster12 fqdn=gluster12.example.com. ipaddress= reverse=

gluster21 fqdn=gluster21.example.com. ipaddress= reverse=
gluster22 fqdn=gluster22.example.com. ipaddress= reverse=

rhev11 fqdn=rhev11.example.com. ipaddress= reverse=
rhev12 fqdn=rhev12.example.com. ipaddress= reverse=

rhev21 fqdn=rhev21.example.com. ipaddress= reverse=
rhev22 fqdn=rhev22.example.com. ipaddress= reverse=

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

 - 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 ]
 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
echo $1
echo $2
echo $3
nsupdate -k /etc/rndc.key << EOF
update add $1 3600 A $2
update add $3 3600 PTR $1

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.


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