Ansible 101 for networking
From an inventory and an ad-hoc command to roles, a vaulted password, an execution environment and a CI pipeline, against a leaf-spine fabric of CapeOS switches with the CLI you already know. One chapter per post, in your browser, nothing to install.
First lab here? Meet the lab shows how every lab works, once.
This is the hands-on side of the series Ansible 101 for networking. The posts explain the ideas. This lab makes you use them, on three switches, with a checker that reads the switches and tells you when you’re right.
The switches run CapeOS Bolt, a lab network OS with the command line most network engineers learned first:
show running-config, configure terminal, interface GigabitEthernet1, write memory. So everything Ansible
does, you can check by hand with commands you already know, and every config line in a playbook is a line you’d
type yourself.
What you’ll build
- An inventory of a small leaf-spine fabric, and ad-hoc commands that ask every switch a question at once.
- Playbooks that set the contact, banners, NTP, locations and ports, and prove they’re idempotent: run twice, change nothing.
- Variables in
group_varsandhost_vars, so one playbook configures every switch differently. - Loops over port data, a
whenfor uplinks, and a Jinja2 report of the whole fabric. - Handlers that save config only when something changed, tags to run part of a playbook, and
block/rescuefor a change that fails halfway. - Two roles with defaults you can override, the switch password in ansible-vault, and the whole thing running in an execution environment, lint-clean, in a CI pipeline.
How it works
Every task follows the same five steps, so you always know where you are:
- Explain: the idea, in plain words, and why it matters.
- Show: what the thing looks like, with every part named.
- Follow along: you build the first piece with the guide, step by step. Every step says which file you’re in, and shows what you should see.
- Challenge: you build the next piece on your own. Hints if you need them, and the solution if you’re stuck.
- Check:
make checkreads the switches and tells you whether you got it, challenge included.
Tasks are numbered chapter.task. All the files you edit are in the lab folder in your codespace; the top of each task lists them, and Ctrl+P (Cmd+P on a Mac) plus the file name opens one.
make run T=2.1 # run the task's playbook or command
make check T=2.1 # verify it against the switches
make check CH=2 # the whole chapter
make solve T=2.1 # stuck? copy that task's finished files into your lab
Start at any chapter. make skip CH=4 copies in the finished files of chapters 1 to 3; run their playbooks
and pick up at 4.1. make up gives you fresh switches any time; your files stay, and re-running your
playbooks puts the config back, which is the whole point of Ansible.
Quick start
- Open the lab and sign in with GitHub. The lab is your own copy, in a codespace on your GitHub account's free allowance (what that means). The first start takes a few minutes; coming back takes about one.
- The lab opens in your browser: this guide on the left, VS Code on the right, and the network's state at the top. Wait for it to say network up.
- Follow task 1.1. Check task, under the guide, tells you when you've got it right.
Finished? Finish lab, top right, deletes the codespace. Left it open? It stops itself after 20 minutes without activity, and Open the lab picks up where you were.
Advanced: run it on your laptop Docker, Git and VS Code
$ git clone https://gitlab.com/networkdoodles-labs/ansible-101.git
$ cd ansible-101
$ code . # then: Reopen in Container
$ make check T=1.1Same dev container, on your own Docker (6 GB of memory, 8 GB in part 5). No dev container? make up and make ee run it with Docker, Python 3.12+ and make. Step by step: the advanced setup.
8 chapters · 18 tasks
- 1.1 Meet the labOpen the lab, find your way around it, and prove all three switches answer.
- 1.2 Write the inventoryList the three switches in groups Ansible understands, and give the fabric the settings to reach them.
- 1.3 Ask the switchesUse ad-hoc commands to find each switch’s software version and leaf1’s serial number, and write them down.
- 3.1 group_vars and host_varsMove the data out of the inventory and playbooks: connection settings and NTP for the fabric, a location per switch, and one playbook that uses them.
- 3.2 FactsAsk each switch for its version and serial number with a facts module, keep the answers in variables, and write them to a file per switch.
- 4.1 Interfaces from dataList every port as data, bring them all up with one looped task, and give only the uplinks a jumbo MTU.
- 4.2 A report from a templateFinish a Jinja2 template that renders a Markdown report of the whole fabric: every switch, its version and location, and its ports with the uplinks marked.
- 5.1 Save only on changeSave each switch’s config with a handler that runs once, only when something actually changed.
- 5.2 TagsTag the interface tasks so you can run just the descriptions, or just the MTU.
- 5.3 When it goes wrongMake a two-step change that fails halfway leave the uplinks exactly as it found them, and report what it ended up with.
- 8.1 Execution environmentsBuild an execution environment with the lab’s collections, and run site.yml inside it with ansible-navigator.
- 8.2 ansible-lintMake the legacy playbook pass ansible-lint’s production profile, without silencing a single rule.
- 8.3 A CI jobAdd the pipeline jobs that lint and syntax-check every push.