Idempotent network automation
Define idempotence through an outcome
An idempotent operation has the same intended effect when repeated as when performed once. Setting a description to a fixed value is different from appending a word every time. For a configuration workflow, aim for convergence: when the owned fields already match the desired values, the next run produces no change. Idempotence is not the same as read-only behavior or identical responses. A second request can produce a new audit entry even if the configuration is unchanged. HTTP defines PUT and DELETE as idempotent methods; POST is not generally idempotent. Verify the actual operation and vendor contract rather than assuming every change request is safe to repeat (RFC 9110, section 9.2.2).
Compare only owned fields
Our mock planner owns two interface fields: description and mtu. The inventory says R1 eth1 currently has description uplink-old, mtu 1500 and enabled true. Desired values are description uplink-core and mtu 1500. A minimal diff changes description only. It must not delete enabled, which this automation does not own. If the desired description is absent, that means no instruction for that field, not deletion. Explicit deletion requires a separate reviewed operation. Use stable device and interface identities, and do not compare text representations if the underlying typed values are what matter.
Plan, apply to a copy, plan again
The pseudocode below validates all desired keys and types, refuses missing owned observations, then emits only unequal values. The classroom apply function updates an in-memory copy, not a device. Apply the description change and compare again: the second plan is empty. If the intended MTU changes to 1600, the next plan contains only mtu:1600. If a desired value is 1600 as a string, reject it rather than silently converting it. Validate the entire desired object before applying anything. Real device defaults, ordering, schema constraints and replacement semantics can make comparison more complex.
observed = {description: uplink-old, mtu: 1500, enabled: true}desired = {description: uplink-core, mtu: 1500}validate desired; require observations for desired fieldsplan = {description: uplink-core}apply plan to a copy; preserve enabledsecond plan = {}Deal with timeout and concurrent changes
Suppose the plan was reviewed against revision 7, but a read immediately before applying finds revision 8. Stop and regenerate the diff; another operator may have changed an owned field. If a server supports conditional updates, use the actual version mechanism, such as a documented HTTP If-Match precondition with a strong ETag. Our mock revision is not a real ETag. If a write response is lost, the write may already have succeeded. Read back the state through an authorized channel before deciding what to do. Limit retry attempts, use backoff for transient failures, and do not retry an uncertain non-idempotent operation blindly. Authentication and validation errors need correction, not repeated traffic.
Review the plan as a team
Store the desired state and templates in version control, with review and a clear ownership boundary. Keep secret values and raw sensitive diffs out of Git. Ansible check and diff modes can help review a plan, but support differs by module and tasks may override check mode. Use only a verified offline or approved isolated test environment; do not infer that a dry-run flag prevents every possible side effect. For this lesson the test is completely local: compare dictionaries and copy values. A successful second empty diff proves only convergence of the mock planner under its assumptions, not physical reachability or production safety.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Idempotent network automation Quiz
Reason about management APIs, convergence and safe change validation.
Desired description is uplink-core and MTU is unchanged. What is the minimal plan?
After applying the plan to a copy, what should a second plan contain?
Desired MTU is a JSON string "1600". What should the mock planner do?
Reviewed revision 7 is now revision 8. What next?
A non-idempotent write times out. What is safest?