Site icon UnixArena

Automation – Add DNS A Record using Ansible Playbook

Ansible - Widows DNS A record creation

Ansible - Widows DNS A record creation

DNS resource records added automatically when windows instances is registered on domain. For Linux instances, you have to add the records manually on DNS server. Let’s explorer that how to create A records in DNS using Ansible Playbook. This playbook can be integrated as part of server provisioning workflow to speed up the build process.

Ansible Version – ansible 2.7.8

Step by Step Procedure:

1. Login to Ansible server.

2. Create a ansible playbook to create DNS records. Depends on environment and security restrictions, you need to find an option that could work for you.

DNS Server : DNSServerzone1
Operating System: Windows 2016 / 2012

Playbook:

---
- hosts: DNSServerzone1
  gather_facts: no 
  tasks:  
   - name: Add A record for newly build server
     win_shell: "Add-DnsServerResourceRecordA -Name {{ServerName}} -ZoneName {{Domain}} -AllowUpdateAny -IPv4Address {{IPAddress}} -TimeToLive 01:00:00"
     when: DOMAIN == "{{Domain}}" 

Variables:

3. Create windows DNS server inventory according to the server security settings. In my example, DNS servers are allowed to communicate using 5985 (http) port for winrm.

Example:

[wintelDNS]
DNSServerzone1.dc.net

[wintelDNS:vars]
 ansible_connection=winrm
 ansible_winrm_server_cert_validation=ignore
 ansible_winrm_transport=kerberos
 ansible_ssh_port=5985
 ansible_user=ADMIN@DC.NET
 ansible_password=ADMINPASS 

4. Execute the playbook against the DNS server inventory to create the required A record.

# ansible-playbook -i inventory playbook_name -e ServerName=SERVER_NAME -e Domain=DC.NET -e IPAddress=192.168.3.98

5. We have successfully added A Records in DNS server. Records will take few minutes to populate in other DNS servers.

Ansible too have pre-written module(win_dns_record ) to fulfill this requirement. Please checkout in Ansible documentation for more information.

Exit mobile version