Search⌘ K
AI Features

Ansible Handlers

Explore how Ansible handlers enhance automation by executing event-driven tasks only when triggered by specific changes in your playbooks. Learn to define, notify, and forcibly execute handlers to manage services and configurations efficiently, including practical examples like restarting web servers after updates.

What are handlers?

Handlers are event-driven tasks that are only executed in response to specific events or conditions. These events are typically associated with changes made by tasks, such as configuration file updates or package installations. Handlers are usually triggered during or after the execution of tasks within a playbook. A task notifies a handler by name using the notify attribute, and when this is done, Ansible queues the handler to be executed after all tasks run. Handlers ensure that actions are taken only in response to relevant events, adding a level of control and automation that are only based on specific triggers.

Here is a typical syntax of a handler:

YAML
---
# Initial configuration for our play
handlers:
- name: name_of_handler # Define the handler's name
module_name:
parameter1: value1
parameter2: value2

In the code snippet above, we define a handler named name_of_handler with the corresponding module name to use and the parameters. We can then call this handler from a task by using the notify ...