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.
More on Playbooks, Plays, Tasks and Handlers
What is a playbook?BeginnerWhat are the main keys of a play?BeginnerWhy should every task have a `name`?BeginnerWhich playbook flags help you inspect and debug before a real run?BeginnerWhat are the behaviours and gotchas of handlers?IntermediateHow do you run handlers immediately instead of at the end of the play?Intermediate