online posts 16
ansible 101 for networking

task 5.3 · chapter 5 of 8: handlers, tags, errors · 25 min

When it goes wrong

objective

Make a two-step change that fails halfway leave the uplinks exactly as it found them, and report what it ended up with.

you edit, in the labplaybooks/safe_change.yml

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

given
Known-good MTU9100 on GigabitEthernet1, each leaf’s uplink
The failurestep 2 sets an MTU of 99999, which the switch rejects: % Invalid input
1 explain

What happens when a change fails halfway, and how do block and rescue fix it?

By default, when a task fails on a host, Ansible stops working on that host. Whatever the earlier tasks changed stays changed. A two-step change that fails on step 2 leaves the switch half-done: the worst state a network can be in.

Ansible borrows try/catch from programming languages, with three sections:

SectionRuns
block:the change itself, a list of tasks
rescue:only if something in the block failed: put things back
always:whatever happened: report, clean up

If rescue succeeds, the host counts as rescued, not failed, and the play carries on.

2 show

What does a block with rescue and always look like?

tasks:
  - name: Change the uplinks, then roll back if anything fails
    block:                          # try
      - name: Step 1 · ...
      - name: Step 2 · ...
    rescue:                         # if anything above failed
      - name: Roll back
    always:                         # no matter what
      - name: Report

block, rescue and always sit at the same indentation: they’re three keys of one task. Inside rescue, ansible_failed_task is the task that failed (its .name is handy in a message), and ansible_failed_result holds the error the switch gave.

3 follow along

See it fail, then rescue it

  1. Run the playbook as it is, and watch it go wrong:

    run in the terminal
    make run T=5.3
    

    Step 1 changes the MTU to 1500. Step 2 fails: the switch answers % Invalid input detected, and Ansible reports it. The recap says failed=1 for both leaves.

  2. Look at the damage: click leaf1 on the Topology tab, then in its tab show running-config interface GigabitEthernet1. There’s no mtu 9100 line any more: it’s back at the default, 1500.

  3. Add a rescue: section that puts the known-good MTU back and says what happened. It goes at the same indentation as block:, after the block’s last task:

    editplaybooks/safe_change.ymlCtrl+P → safe_change.yml
          rescue:
            - name: Roll back to the known-good MTU
              cisco.ios.ios_interfaces:
                config:
                  - name: GigabitEthernet1
                    mtu: 9100
    
            - name: Say what happened
              ansible.builtin.debug:
                msg: "{{ inventory_hostname }}: '{{ ansible_failed_task.name }}' failed, so the MTU is back to 9100"
    
  4. Run it again. Step 2 still fails, the rescue runs, and the recap now says rescued=1 and failed=0. The MTU is back to 9100.

4 challenge

Always report the result

Add an always: section that reads the uplink’s config back from the switch and shows it, so every run ends with proof of the state it left behind, whether the change worked or not.

hint

Reading is a show command, with cisco.ios.ios_command like chapter 1: show running-config interface GigabitEthernet1. Keep its result with register: (any name that isn’t one of Ansible’s own words: uplink_config works, port doesn’t), then debug its stdout_lines.

hint

always: lines up with block: and rescue:.

show the solutionplaybooks/safe_change.yml

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

      always:
        - name: Read the port's config back
          cisco.ios.ios_command:
            commands:
              - show running-config interface GigabitEthernet1
          register: uplink_config

        - name: Show it
          ansible.builtin.debug:
            var: uplink_config.stdout_lines[0]

5 check: run it, then prove it

$ make run T=5.3
$ make check T=5.3

when it's right, you'll see

╭─ task 5.3 · when it goes wrong
│  ✓ playbooks/safe_change.yml finishes, even though its change fails
│  ✓ the rescue section ran
│  ✓ leaf1 GigabitEthernet1 is back to its known-good mtu 9100 (got 9100)
│  ✓ leaf2 GigabitEthernet1 is back to its known-good mtu 9100 (got 9100)
│  ✓ the challenge: the block has an always section
│  ✓ ...and it ran, after the rescue
╰─ 6/6 checks passed  verified