>_ IzzyLab

Hands-on task

This task is step 12 of the course “Terraform and Ansible”

Ansible: handler on change

medium~15 min

Write a playbook /root/handler.yml: a task creates the file /etc/svc.conf (the copy module) and on its change notifies a handler that creates a marker file /var/run/reloaded. After the run both files must exist. The playbook must work from scratch, when neither file exists, and on a second run without a config change the handler must not fire. Write the playbook in the Editor tab and run ansible-playbook in the terminal.

What you'll practise:
  • ansible
  • handlers
  • notify
  • copy module
  • idempotency

Topic breakdown

A handler in Ansible is a special task that runs not on its own, but only when a regular task has notified it via notify. The typical pattern: a task changed a config and asked the handler to restart the service; but if thanks to idempotency nothing changed, the handler won't fire. The handlers: section lives at the same level as tasks:, and the string in notify must match the handler's name word for word — otherwise Ansible won't find it.

What's checked

  1. the playbook runs from scratch
  2. /etc/svc.conf and the marker /var/run/reloaded are created
  3. the handler doesn't fire when the config is unchanged

What it looks like