Network Automation with Ansible

Inventory design, idempotent network modules, config diffing and a rollout pattern that will not take down a campus.

Published Apr 7, 2026Updated Aug 24, 202610 min read5 views
  • ansible
  • automation
  • cisco
  • networking

Inventory is the design document

YAML
all:
  children:
    campus:
      children:
        access:
          hosts:
            sw-a-01: { ansible_host: 10.10.10.21 }
            sw-a-02: { ansible_host: 10.10.10.22 }
        distribution:
          hosts:
            dsw-01: { ansible_host: 10.10.10.11 }
      vars:
        ansible_network_os: cisco.ios.ios
        ansible_connection: ansible.netcommon.network_cli

Prefer resource modules over raw commands

YAML
- name: Ensure access VLANs exist
  cisco.ios.ios_vlans:
    config:
      - vlan_id: 110
        name: SITE1-USER
      - vlan_id: 120
        name: SITE1-VOICE
    state: merged

- name: Ensure NTP servers
  cisco.ios.ios_ntp_global:
    config:
      servers:
        - server: 10.10.10.53
        - server: 10.10.10.54
    state: replaced

Diff before you change

Bash
ansible-playbook site.yml --limit access --check --diff
ansible-playbook site.yml --limit sw-a-01 --diff

Rollout pattern

  1. Apply to one canary device and validate with an assertion playbook.

  2. Roll out per closet with serial: 1 and a health gate between batches.

  3. Save the configuration only after validation passes.

YAML
- name: Validate reachability after change
  ansible.builtin.assert:
    that:
      - "'up' in interface_state.stdout[0]"
    fail_msg: "Uplink down after change — halting rollout"

Guardrails

  • Never store enable secrets in the inventory; use a vault or an external secret store.

  • Keep a scheduled config backup job independent of the change playbooks.

  • Make every playbook safe to run twice.

Back to all docs