Network Services Automation with Ansible
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
graph LR
Vars[group_vars] --> Tpl[Jinja2 templates]
Tpl --> Ans[Ansible]
Ans -->|deploy + validate| DNS[BIND9]
Ans -->|deploy + validate| DHCP[ISC DHCP]
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 --diffPreviews 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.yml03
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
graph LR
A[01 Template] -->|done| B[02 Validate + deploy]
B -->|done| C[03 Idempotent]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
dig @ns1 web.lab +short && ansible-playbook -i inventory.ini dns-dhcp.yml --checkDNS 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
- Add device config automation for switches/routers.
- Run from CI/CD with approvals.
- Combine with Complete Network Implementation.
Key Results
- Codified DNS and DHCP from templated variables.
- Gated reloads behind config validation.
- Achieved idempotent runs (
changed=0on repeat). - Made network services version-controlled and reproducible.