You will deploy and manage DNS and DHCP from code with Ansible, generating zone and scope files from templates. By the end network services are reproducible, version-controlled, and validated on every run.

Learning Objectives

  • Template DNS zones and DHCP scopes from variables.
  • Deploy services idempotently with Ansible.
  • Validate configuration before reloading.
  • Time: ~12 hours · Difficulty: Advanced · Prereqs: the DNS, DHCP, and automation labs.

Architecture Overview

Environment Setup

  • Ansible control node with SSH to service hosts.
  • BIND9 and ISC DHCP target hosts.
  • Variables describing zones, records, and scopes.

Step-by-Step Execution

01
Template the zone and scope files
ansible-playbook -i inventory.ini dns-dhcp.yml --check --diff
Previews the rendered config before applying.
02
Validate before reload

The playbook runs named-checkzone and dhcpd -t before reloading, so a bad template never takes a service down.

ansible-playbook -i inventory.ini dns-dhcp.yml
03
Confirm idempotency
$ ansible-playbook -i inventory.ini dns-dhcp.yml
PLAY RECAP ns1: ok=9 changed=0 dhcp1: ok=7 changed=0

Progress So Far

Testing & Validation

dig @ns1 web.lab +short && ansible-playbook -i inventory.ini dns-dhcp.yml --check

DNS should resolve and a check run should report no changes. If so, your network services are fully codified and idempotent.

Troubleshooting
  • changed=1 every run: templates produce non-deterministic output; sort and normalize.
  • Service down after deploy: ensure validation tasks gate the reload handler.
  • Stale zone: bump the SOA serial in the template on each change.

Extension Ideas

Key Results

  • Codified DNS and DHCP from templated variables.
  • Gated reloads behind config validation.
  • Achieved idempotent runs (changed=0 on repeat).
  • Made network services version-controlled and reproducible.