Articles

Ansible for network automation: intent, idempotency and the gaps

A play can declare the end state; getting the device there is the other half of the job.

Reading: 5 minNetworking

Article cover: Ansible for network automation: intent, idempotency and the gaps

Ansible is agentless: the control node pushes work to a device over an existing connection and installs nothing on it. A playbook is an ordered list of plays, each targeting a set of hosts and running tasks in order; each task calls a module (ansible-playbooks-intro.txt line 35). The intent is declarative — the play names the end state, the module reaches it — but the guarantee is conditional. Most modules check whether the desired state is already present and do nothing when it is, yet the documentation warns that “not all playbooks and not all modules behave this way” (line 90). Network devices break that condition often, so “the play succeeded” and “the device is as declared” are two different claims.

The model: inventory, play, task, module, no agent

Ansible builds its inventory from one or more sources: the hosts to manage and the variables attached to them, grouped and selected with patterns (ansible-inventory.txt lines 21–22). The default file is /etc/ansible/hosts; another is named with -i (line 24). A play targets hosts by pattern and runs tasks in order from top to bottom, each executing a module (ansible-playbooks-intro.txt lines 35, 39). Nothing runs on the device: Ansible reaches it over an existing channel, SSH or an API, and for network equipment the inventory carries the platform identifier ansible_network_os and the network connection plugin beside the SSH user (ansible-network-inventory.txt lines 94, 221–222).

control node ── playbook: declared intent, one group of hosts
      │
      ▼
   task ──▶ module ──▶ connection plugin ──▶ device
                              (SSH / API)
      ▲
      └── per task, per host: ok · changed · failed · unreachable

Idempotency: “changed” is a report, not a promise

Idempotency is the property that matters. Most modules “check whether the desired final state has already been achieved and exit without performing any actions if that state has been achieved” (ansible-playbooks-intro.txt line 90); the run report states, per task and per machine, whether it succeeded and whether it made a change (line 86). That column carries the defect: a play reporting changed on every run against a device that has not moved means the module cannot see the state it is meant to keep, so the declaration is reapplied every pass and whatever changed underneath stays invisible. On a network device this is often the module’s design — a CLI module pushes a line and the device decides.

Declarative and imperative on the same device

The declarative promise is the first capability the documentation lists: playbooks “Declare configurations” (ansible-playbooks-intro.txt line 25). Underneath, much network automation is imperative, and the two styles show in what the network collections build on. Netmiko is a “Multi-vendor library to simplify CLI connections to network devices” (netmiko-readme.md line 12) whose work is “primarily concerned with gathering output from show commands and with making configuration changes” (line 23); it sends a configuration set that “will automatically enter into config mode” (line 133) — commands, not a state. NAPALM sits one level up, “a Python library that implements a set of functions to interact with different router vendor devices using a unified API” (napalm-readme.md line 9), with methods “to manipulate configurations or to retrieve data” (line 13). A declarative play can rest on either; which sits underneath decides whether a second run is a check or a re-push.

Collecting facts is not applying intent

Reading state and writing state are separate jobs. A complete network inventory “can serve as the source of truth for your network”, and “a single playbook can maintain hundreds of network devices with a single command” (ansible-network-inventory.txt line 21). Keeping them apart is what lets the collected state be compared with the declared one — the drift sheet’s subject. A device that does not answer is unknown, not healthy.

Check mode, and what it cannot see

Check mode “allows you to execute a playbook without applying any alterations to your systems”; instead of making changes, it “provides a report on the changes it would have made” (ansible-playbooks-intro.txt lines 102, 108). Its usefulness is bounded by the module: a dry run predicts a change only where the module can read current state and compute the difference; where it cannot, the forecast is a guess. The other options — --diff, --list-hosts, --list-tasks and --syntax-check (line 124) — answer narrower questions about the play. None touches the device, so none says what it will look like afterwards: that is post-change validation, and it runs after the change.

Failure is partial by default

Ansible applies a task to every matched host before moving on, and it stops a host on failure: “If a task fails on a host, Ansible removes that host from the rotation for the rest of the playbook” (ansible-playbooks-intro.txt line 84). A play interrupted halfway does not undo what it already applied, and the device is left between the old declaration and the new one — a state no playbook describes. The recoveries belong elsewhere: rollback is the validation-and-rollback sheet’s subject, and the control node, which holds credentials for every device it manages and must be treated as a privileged host, is the CLI-and-credentials sheet’s.

Level and prerequisites

L5 — operational automation. It assumes the device-API and model-driven-interface model of the sibling sheet, and the L1–L2 forwarding foundation. It uses, without re-teaching them, the inventory-and-variables model, credential handling, validation and drift detection.

Where to go next

References

  • Ansible Community Documentation — Ansible playbooks (project documentation, not a standard): the play-and-task model, the fully-qualified collection name rule, the idempotency section, check mode and the verification options.
  • Ansible Community Documentation — How to build your inventory (project documentation): inventory sources, the default /etc/ansible/hosts, the -i option, patterns, and the SSH-by-default connection.
  • Ansible Community Documentation — Build Your Inventory (network getting started, project documentation): the network inventory, ansible_network_os, the network connection plugin, and the inventory as the source of truth.
  • NAPALM — README, napalm-automation/napalm (project documentation): the unified-API description, the connect, configure and retrieve methods, and the vendor caveats.
  • Netmiko — README, ktbyers/netmiko (project documentation): the CLI-connection description, the show-and-configuration focus, and the automatic config-mode entry.
Nodrius Field Kit — cover

Get the Nodrius Field Kit

Enter your email and we'll send you the complete Field Kit: eight technical resources in one download.

Your email address is sent to Nodrius and used to deliver the Field Kit to you by email. Marketing messages are separate and optional: the checkbox does not affect your download, and you are only added to our updates list if you tick it. Privacy Policy.

Privacy & cookies

This site does not use cookies, analytics or tracking, and it does not profile you. There is nothing technical to switch off, so Accept and Reject change nothing about how the site works: either choice lets you browse everything normally.

The only difference is what your browser remembers: two entries in local storage — your choice here, so this notice is not shown again, and a marker that the Field Kit offer has already been made — so the offer is presented once and not on every page. They contain no identifier, they are not shared with anyone, and the site sets no cookie for either.

What we do with your data
How your email address is used when you ask for a Resource, how long it is kept and which rights you have — in the Privacy Policy.
What the site stores in your browser
Two entries in local storage — your choice on this notice and whether the Field Kit offer has been shown — and no cookies, no session storage, no third-party content: in the Cookie Policy.