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.
playbooks/safe_change.ymlopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Known-good MTU | 9100 on GigabitEthernet1, each leaf’s uplink |
|---|---|
| The failure | step 2 sets an MTU of 99999, which the switch rejects: % Invalid input |
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:
| Section | Runs |
|---|---|
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.
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.
See it fail, then rescue it
Run the playbook as it is, and watch it go wrong:
run in the terminalmake run T=5.3Step 1 changes the MTU to 1500. Step 2 fails: the switch answers
% Invalid input detected, and Ansible reports it. The recap saysfailed=1for both leaves.Look at the damage: click leaf1 on the Topology tab, then in its tab
show running-config interface GigabitEthernet1. There’s nomtu 9100line any more: it’s back at the default, 1500.Add a
rescue:section that puts the known-good MTU back and says what happened. It goes at the same indentation asblock:, after the block’s last task:editplaybooks/safe_change.ymlCtrl+P → safe_change.ymlrescue: - 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"Run it again. Step 2 still fails, the rescue runs, and the recap now says
rescued=1andfailed=0. The MTU is back to 9100.
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.3when 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
background: part 5, When a change fails halfway