online posts 16
ansible 101 for networking

task 5.1 · chapter 5 of 8: handlers, tags, errors · 20 min

Save only on change

objective

Save each switch’s config with a handler that runs once, only when something actually changed.

you edit, in the labplaybooks/base.yml

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

given
Save commandwrite memory, with cisco.ios.ios_command
Handler nameSave the running config
1 explain

What is an Ansible handler?

You know this one from the CLI: a switch has two configs, the running-config, which is what it’s doing now, and the startup-config, which is what it loads after a reboot. Your playbooks change the running-config. Reboot a switch and those changes are gone, unless someone types write memory.

Saving after every run would work, but it’s noisy and slow, and it saves even when nothing changed. A handler does it properly:

  • a handler is a task that only runs when another task notifies it;
  • a task notifies it with notify:, and the notification only happens if that task changed something;
  • however many tasks notify it, the handler runs once, at the end of the play.

So: something changed, save once. Nothing changed, do nothing.

2 show

Where do handlers and notify go?

- name: Base config
  hosts: fabric
  gather_facts: false

  tasks:
    - name: NTP servers, from group_vars
      cisco.ios.ios_ntp_global:
        config: [...]
      notify: Save the running config        # a task keyword: "if I change something, tell this handler"

  handlers:                                  # same level as tasks:
    - name: Save the running config          # must match the notify text exactly
      cisco.ios.ios_command:
        commands:
          - write memory                     # the same command you'd type

The handler’s name is its address. A typo in either place and Ansible stops the play with an error: the requested handler was not found. Annoying, and exactly what you want: a save that silently never ran would be worse.

3 follow along

The handler, and the NTP task notifies it

  1. In the base playbook, add notify: to the NTP task, lined up with cisco.ios.ios_ntp_global::

    editplaybooks/base.ymlCtrl+P → base.yml
        - name: NTP servers, from group_vars
          cisco.ios.ios_ntp_global:
            config:
              servers: "{{ ntp_servers }}"
          notify: Save the running config
    
  2. At the bottom of the file, add the handlers: section, at the same indentation as tasks: (2 spaces in):

    editplaybooks/base.ymlCtrl+P → base.yml
      handlers:
        - name: Save the running config
          cisco.ios.ios_command:
            commands:
              - write memory
    
  3. Run it with make run T=5.1. Nothing happens, and that’s right: your switches already have NTP from chapter 3, so nothing changed and nothing was notified.

  4. Make a change for it to notice. Log in (click leaf1 on the Topology tab) and take one NTP server away:

    run in the leaf1 tab
    leaf1# configure terminal
    leaf1(config)# no ntp server 162.159.200.1
    leaf1(config)# end
    leaf1# exit
    

    Run the playbook again: the server comes back on leaf1, and at the end you see RUNNING HANDLER [Save the running config], for leaf1 only. show startup-config | include ntp on leaf1 now has both servers: saved.

4 challenge

The location notifies it too

If someone moves a switch, its new location should be saved too. Make the location task notify the same handler, so a location change on its own also saves the config.

Test it yourself: change leaf2’s location in its host_vars file, run the playbook, and look for the handler. Then put the location back and run it again.

hint

It’s one line, on the second task, in the same place as on the first.

hint

Both tasks changed on the same run? The handler still runs once. That’s the other half of the point.

show the solutionplaybooks/base.yml

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

    - name: Where the switch lives, from host_vars
      cisco.ios.ios_config:
        lines:
          - "snmp-server location {{ location }}"
      notify: Save the running config

5 check: run it, then prove it

$ make run T=5.1
$ make check T=5.1

when it's right, you'll see

╭─ task 5.1 · save only on change
│  ✓ playbooks/base.yml runs cleanly (the checker removed NTP first, so there's a change)
│  ✓ something changed, so the save handler ran
│  ✓ and it ran once, at the end, not once per task
│  ✓ spine1's startup-config has the NTP servers: the handler saved it
│  ✓ leaf1's startup-config has the NTP servers: the handler saved it
│  ✓ leaf2's startup-config has the NTP servers: the handler saved it
│  ✓ a second run changes nothing, so the handler stays quiet
│  ✓ the challenge: a location change alone also runs the save handler
╰─ 8/8 checks passed  verified