online posts 16
ansible 101 for networking

task 2.2 · chapter 2 of 8: your first playbook · 20 min

Check mode first

objective

Preview each change with –check -v before you make it: a domain name, then LLDP.

you edit, in the labplaybooks/hello.yml

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

given
Domainip domain name lab.networkdoodles.com
LLDPlldp run
1 explain

What does Ansible’s –check do on a switch?

On a real network you don’t run a change and hope. You find out what it would do first.

Ansible has that built in. --check (check mode) does everything except change anything: Ansible logs in, reads the running config, works out which lines are missing, and stops there. The switch is untouched.

Add -v (verbose) and each task also prints the exact commands it would send. That list is your change review: the same lines you’d paste into a change ticket, worked out by Ansible from what’s actually on each switch. It’s the habit that separates automation people trust from automation people fear.

You may know --diff too. On servers it shows a file’s before and after. The switch modules don’t print a line-by-line diff in check mode, so for switches, -v is the flag that tells you what’s coming.

2 show

How do you read a check-mode run?

With --check -v, a task that would change something prints its result, with the commands in it:

TASK [The domain the switches live in] ******************************
changed: [leaf1] =>
    banners: {}
    changed: true
    commands:
    - ip domain name lab.networkdoodles.com
    updates:
    - ip domain name lab.networkdoodles.com
  • commands: is what it would type in config mode, on this switch. (updates is the same list again; ignore it.)
  • changed: in check mode means “would change”. Nothing has, yet.
  • A task that’s already right says ok: and has no commands.
  • At the end, the PLAY RECAP counts what would change on each switch.
3 follow along

The domain name, previewed first

  1. Add a third task to the playbook, after the banner task from 2.1:

    editplaybooks/hello.ymlCtrl+P → hello.yml
        - name: The domain the switches live in
          cisco.ios.ios_config:
            lines:
              - ip domain name lab.networkdoodles.com
    
  2. Don’t run it yet. Preview it:

    run in the terminal
    ansible-playbook playbooks/hello.yml --check -v
    

    The contact and banner tasks say ok: they’re already right. The new task says changed, with ip domain name lab.networkdoodles.com under commands:. Nothing has actually changed: click leaf1 on the Topology tab, then in its tab show running-config | include domain prints nothing.

  3. Happy with it? Run it for real, without the flags:

    run in the terminal
    ansible-playbook playbooks/hello.yml
    
  4. Preview once more. Every task says ok and the recap says changed=0: there’s nothing left to do.

4 challenge

LLDP, the same way

Add a fourth task that turns on LLDP, so every switch learns which neighbor is on which port. The config line is lldp run. Preview it with --check -v, read the commands, then apply it.

hint

It’s the domain task’s shape: a new name and a new line.

hint

LLDP only finds neighbors on ports that are up, and the lab’s ports are still shutdown. Chapter 4 brings them up; then show lldp neighbors on any switch shows who’s on the other end of each cable.

show the solutionplaybooks/hello.yml

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

    - name: LLDP, so each switch learns its neighbors
      cisco.ios.ios_config:
        lines:
          - lldp run

5 check: run it, then prove it

$ ansible-playbook playbooks/hello.yml --check -v
$ make check T=2.2

when it's right, you'll see

╭─ task 2.2 · check mode first
│  ✓ spine1: ip domain name lab.networkdoodles.com
│  ✓ the challenge: spine1 runs LLDP (lldp run)
│  ✓ leaf1: ip domain name lab.networkdoodles.com
│  ✓ the challenge: leaf1 runs LLDP (lldp run)
│  ✓ leaf2: ip domain name lab.networkdoodles.com
│  ✓ the challenge: leaf2 runs LLDP (lldp run)
│  ✓ --check runs and finds nothing left to change
╰─ 7/7 checks passed  verified