Interview Prep/Ansible/Playbooks, Plays, Tasks and Handlers

What is a handler and how is it triggered?

BeginnerConceptPlaybooks, Plays, Tasks and Handlers
Answer

A handler is a task that runs only when notified, typically to restart or reload a service after its config changed. A task notifies a handler with notify, and the notification only fires if that task reported changed.

tasks:
  - ansible.builtin.template:
      src: nginx.conf.j2
      dest: /etc/nginx/nginx.conf
    notify: Reload nginx
handlers:
  - name: Reload nginx
    ansible.builtin.service:
      name: nginx
      state: reloaded

No config change means no needless restart, which is the whole point.

All Ansible questions Roadmap: Ansible

More on Playbooks, Plays, Tasks and Handlers