intermediate

Network Automation: What to Automate, and How to Do It Safely

Why automation exists, what is worth automating first, the ideas that make it safe (idempotency, small blast radius, dry runs, rollback), and a realistic path from CLI habits to code.

25 min

Why automate a network at all?

Manual work by command line does not scale. Changing an NTP server on 400 switches by hand means 400 logins, 400 chances to mistype, and no record of what was actually done. Studies of outages routinely find that a large share begin with a human change. Automation is not mainly about speed. Its real benefits are:

  • Consistency: every device gets the same, reviewed change.
  • Auditability: the change is a file in version control, with an author and a reason.
  • Repeatability: the same job can be run again, in test and then in production.
  • Time for better work: engineers stop typing the same commands and start designing.

It also has costs. Automation takes time to build, has bugs of its own, and a wrong script is a wrong change delivered to every device at once. Part of being good at it is knowing what to automate and how to contain a mistake.

From CLI habits to code: a realistic path

Most teams move through stages. You do not have to jump to the end.

  1. Scripts that read: collect show output, back up configurations, build an inventory. No risk to the network.
  2. Templates: generate configuration from a template and variables instead of typing it. Review the output before it is applied.
  3. Controlled change: push configuration with checks before and after, to a small group first.
  4. Pipelines: changes are proposed in version control, tested automatically, reviewed, and deployed by a pipeline.
  5. Intent and source of truth: a database of what the network should look like drives configuration and checks drift.

This course follows the same order: Python and data formats, then Git and APIs, then libraries (Netmiko, NAPALM, Nornir), Ansible, templates and secrets, model-driven interfaces, testing, Terraform and finally CI/CD.

What to automate first

Good first candidates are boring, frequent and low risk:

Task Risk Why it is a good start
Configuration backups None (read-only) Instant value, and a safety net for everything after
Inventory and version audit None You discover what you really have
Compliance checks (is SNMPv2 still enabled? are NTP servers correct?) None Finds problems without changing anything
Generating standard configs for new devices Low, if reviewed Removes copy-and-paste errors
Bulk changes to a single setting (a DNS server, a banner) Medium A useful first real change, easy to verify
Large changes to routing or security policy High Do these last, with testing and staged rollout

Rule of thumb: automate a task you already do reliably by hand, so you know what correct looks like.

Four ideas that make automation safe

1. Idempotency

An idempotent operation can be run any number of times and ends in the same state. “Ensure NTP server 10.0.0.1 is configured” is idempotent; “append ntp server 10.0.0.1 to the config” is not, because running it twice may add it twice. Tools such as Ansible and Terraform are declarative for this reason: you describe the end state, they work out the difference. When you write your own scripts, check the current state first and only change what differs.

2. A small blast radius

Roll out in stages: one lab device, then one low-importance production device, then a site, then everything. Many tools let you set a batch size (serial in Ansible) and stop after the first failures. The aim is that a wrong change is discovered while it affects one device, not a thousand.

3. Look before you leap: dry runs and diffs

Always be able to see what would change before it does. Ansible has check mode (--check --diff), NAPALM can compare and show a configuration diff before commit, and Terraform has plan. Make the diff part of the review.

4. A way back

Before changing a device, capture its configuration. Prefer methods with built-in safety: commit-confirm on Junos (the change reverts unless you confirm it), configuration replace with rollback on supported platforms, or your own saved backup. Test the rollback in the lab, because a rollback that has never been tried is a hope.

Habits of reliable automation

  • Treat code as code: keep it in Git, review changes, write meaningful commit messages.
  • Never hard-code secrets: passwords, API tokens and keys do not belong in scripts or repositories. Use environment variables, a vault or your CI system’s secret store (covered later in this course).
  • Log everything: which devices, what change, who ran it, result.
  • Make scripts fail loudly: an error on one device should be reported, not swallowed.
  • Handle one thing well: small tools that do one job are easier to test and trust.
  • Document the intent in comments and README files. The person maintaining it in a year may be you.

A first exercise

Pick one repetitive task you or your team do regularly, for example “log in to each switch and check the NTP status”. Write down:

  1. The exact manual steps.
  2. What the output looks like when everything is correct.
  3. What would make the task unsafe to automate.
  4. How you would confirm the automated version did the same thing.

You will turn this into code step by step in the following lessons.

A colleague proposes a script that appends 'logging host 10.1.1.5' to every device's configuration nightly. What is wrong with this design, and how would you improve it?

It is not idempotent: each nightly run adds another line (or fails if the device rejects duplicates), so configurations drift and may break. Improve it by checking whether the line already exists and adding it only if missing, or by using a declarative tool that ensures the logging host is present. Also test on one device first, produce a diff for review, and keep a backup so you can revert.

Can You Explain It?

Close the lesson and answer these without looking back. The goal isn't the wording — it's whether you've formed a usable mental model.

  • In your own words, what does idempotent mean, and why does it matter for automation?
  • Why is a read-only task a good first automation project?
  • What can you do before and during a change to keep its blast radius small?

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 4

What does it mean for an automation task to be idempotent?

Report a problem

About: /lessons/network-automation-mindset-and-safety