online posts 16
ansible 101 for networking

task 1.3 · chapter 1 of 8: hello, ansible · 15 min

Ask the switches

objective

Use ad-hoc commands to find each switch’s software version and leaf1’s serial number, and write them down.

you edit, in the labanswers/1.3.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Modulecisco.ios.ios_command: run show commands on a switch
Versionshow version, the Version on the first line
Serialshow version, the Processor board ID
1 explain

What is an Ansible ad-hoc command?

An ad-hoc command runs one Ansible module against a group of hosts, straight from the terminal. No playbook, no files. It’s how you ask a quick question of a whole network: “what version is everything running?”

A module is a unit of work Ansible knows how to do. Modules come in collections; the lab’s switches are driven by the cisco.ios collection, and the module that runs show commands is cisco.ios.ios_command. It logs in, types the command, and brings back whatever the switch printed. It’s show version on three switches at once, instead of three logins.

2 show

What are the parts of an ad-hoc command?

ansible  fabric  -m cisco.ios.ios_command  -a "commands='show version'"
#        ──────     ──────────────────────     ────────────────────────
#        which      which module               the module's arguments
#        hosts
  • Which hosts: a group or host name from your inventory. fabric is all three switches.
  • -m: the module.
  • -a: its arguments, as name=value. The command has spaces, so it goes in single quotes, and the whole argument in double quotes.

Each switch answers with a block like this, in whatever order they finish:

leaf1 | SUCCESS =>
    changed: false
    stdout:
    - |-
        CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY
        ...
        Processor board ID CAPEQ3I7EKD
        ...

changed: false: a show command never changes anything, and Ansible knows it.

3 follow along

Every switch’s version

  1. Run the command (or make run T=1.3, which runs the same thing):

    run in the terminal
    ansible fabric -m cisco.ios.ios_command -a "commands='show version'"
    
  2. Find Version on the first line of each switch’s block. Check the name at the top of each block: they arrive in whatever order the switches answer.

  3. Open the answers file and fill in the three versions, exactly as printed (just the number), inside quotes:

    editanswers/1.3.ymlCtrl+P → 1.3.yml
    spine1:
      version: "0.11.2"
    leaf1:
      version: "0.11.2"
      serial:
    leaf2:
      version: "0.11.2"
    
  4. Save, and run make check T=1.3. Three check marks for the versions, and one cross: the serial. That’s yours.

4 challenge

leaf1’s serial number

Find leaf1’s serial number and put it in answers/1.3.yml under leaf1: serial:. It’s the Processor board ID in show version. This time, ask only leaf1, and print only the line you need.

hint

Two things change in the command: the host (not the whole fabric this time), and the show command. The CLI filters output with a pipe, the same as when you’re logged in: | include <text>.

hint

It’s a lab switch, so the serial is a made-up placeholder. Copy it exactly anyway.

show the solutionanswers/1.3.yml

or pull it into your lab: make solve T=1.3, then git diff --staged shows what changed

ansible leaf1 -m cisco.ios.ios_command -a "commands='show version | include board'"

Then copy the value after Processor board ID:

leaf1:
  version: "0.11.2"
  serial: CAPEQ3I7EKD

5 check: run it, then prove it

$ make run T=1.3
$ make check T=1.3

when it's right, you'll see

╭─ task 1.3 · ask the switches
│  ✓ answers/1.3.yml has spine1's software version
│  ✓ answers/1.3.yml has leaf1's software version
│  ✓ answers/1.3.yml has leaf2's software version
│  ✓ and leaf1's serial number
╰─ 4/4 checks passed  verified