Sobes.tech
Intern

What are handlers in Ansible and when are they triggered?

sobes.tech AI

Answer from AI

Handlers in Ansible are special tasks that are triggered only when notified by other tasks. They are typically used to perform actions that should only occur upon changes, such as restarting a service after updating its configuration.

When a task changes the state (for example, copying a file with modifications), it can trigger a handler using the notify directive.

Example:

- name: Update configuration file
  copy:
    src: myapp.conf
    dest: /etc/myapp.conf
  notify: Restart myapp

handlers:
  - name: Restart myapp
    service:
      name: myapp
      state: restarted

The handler will only run if the task that triggered it actually made changes. If there were no changes, the handler does not execute.