Ansible: Run Handler Immediately (Force)

Handlers in Ansible are special tasks that only get executed when triggered by another tasks via the notify directive.

By default, handlers run not immediately but only after all the tasks in a particular play have been completed.

This makes sense, as the handler will only run one time, regardless of how many tasks have notified it.

For example, if multiple tasks update a configuration file and notify a handler to restart Nginx, Ansible will run this handler only one time to avoid unnecessary restarts.

Cool Tip: Enable DEBUG mode and increase VERBOSITY in Ansible! Read more →

Force Ansible Handlers To Run Immediately

If you want to force handlers to run immediately, without waiting for the end of the play, add a meta: flush_handlers task:

  - name: "Some task"
    command: ...

  - name: "Flush handlers"
    meta: flush_handlers

  - name: "Some other task"
    command: ...

The meta: flush_handlers task immediately triggers any handlers that have been notified at that point in the play.

Leave a Reply