online posts 16
labs

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_vars and host_vars, so one playbook configures every switch differently.
  • Loops over port data, a when for 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/rescue for 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:

  1. Explain: the idea, in plain words, and why it matters.
  2. Show: what the thing looks like, with every part named.
  3. 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.
  4. Challenge: you build the next piece on your own. Hints if you need them, and the solution if you’re stuck.
  5. Check: make check reads 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

  1. 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.
  2. 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.
  3. 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.1

Same 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

ch 1 Hello, AnsibleMeet the fabric, write an inventory, and ask three switches a question with one command. 30 min lesson 1
  1. 1.1 Meet the labOpen the lab, find your way around it, and prove all three switches answer. 15 min
  2. 1.2 Write the inventoryList the three switches in groups Ansible understands, and give the fabric the settings to reach them. 15 min
  3. 1.3 Ask the switchesUse ad-hoc commands to find each switch’s software version and leaf1’s serial number, and write them down. 15 min
ch 2 Your first playbookPlays, tasks and modules. Config lines on every switch, then proof that a second run changes nothing. 25 min lesson 2make seed CH=2
  1. 2.1 Your first playbookWrite a playbook that sets the same contact and login banner on every switch, and prove a second run changes nothing. 20 min
  2. 2.2 Check mode firstPreview each change with –check -v before you make it: a domain name, then LLDP. 20 min
ch 3 Variables and factsgroup_vars and host_vars, which one wins, and asking the switches for facts you can use. 35 min lesson 3make seed CH=3
  1. 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. 30 min
  2. 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. 25 min
ch 4 Loops, conditionals, templatesEvery port from a list of data, uplinks treated differently, and a report rendered with Jinja2. 35 min lesson 4make seed CH=4
  1. 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. 25 min
  2. 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. 25 min
ch 5 Handlers, tags, errorsSave config only when something changed, run part of a playbook, and recover when a change fails halfway. 30 min lesson 5make seed CH=5
  1. 5.1 Save only on changeSave each switch’s config with a handler that runs once, only when something actually changed. 20 min
  2. 5.2 TagsTag the interface tasks so you can run just the descriptions, or just the MTU. 15 min
  3. 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. 25 min
ch 6 Roles and collectionsTurn the playbooks into two roles, and see why role defaults lose to everything. 35 min lesson 6make seed CH=6
  1. 6.1 Playbooks into rolesTurn the base and interface playbooks into two roles with defaults and a handler, and run the whole fabric from site.yml. 35 min
  2. 6.2 Defaults and overridesGive the leaves their own message of the day, and leaf2 one of its own, without touching the role. 15 min
ch 7 SecretsThe switch password out of Git, into ansible-vault, with the key kept off the repo. 15 min lesson 7make seed CH=7
  1. 7.1 Vault the passwordGet the switch password out of plain text: an encrypted vault file, a reference to it, a key that never reaches Git, and one value encrypted inline. 25 min
ch 8 Run it like productionAn execution environment, ansible-lint’s production profile, and a CI pipeline on every push. 35 min lesson 8make seed CH=8
  1. 8.1 Execution environmentsBuild an execution environment with the lab’s collections, and run site.yml inside it with ansible-navigator. 25 min
  2. 8.2 ansible-lintMake the legacy playbook pass ansible-lint’s production profile, without silencing a single rule. 25 min
  3. 8.3 A CI jobAdd the pipeline jobs that lint and syntax-check every push. 20 min