What does force_handlers: true change when a task fails later in the play?
Handlers that were already notified still run, even though the play aborts on that host because of a later failure.
* force_handlers keeps the notifications that were queued before the failure. *
- hosts: all
force_handlers: true
tasks:
- name: a task which always notifies its handler
ansible.builtin.command: /bin/true
notify: restart the database
- name: a task which fails because the package doesn't exist
ansible.builtin.dnf:
name: notapkg
state: latest
handlers:
- name: restart the database
ansible.builtin.service:
name: mariadb
state: restarted
The first task (command reports changed by default) notifies the handler; the second task fails. Without force_handlers, the host leaves the play at the failure and the queued restart is dropped. That can leave a host with a new configuration file but the old process still running. With force_handlers: true on the play, the restart still happens.
The same setting exists as the command-line option --force-handlers and as force_handlers = True in ansible.cfg. Some errors, such as the host becoming unreachable, can still prevent the handler from running.
Go deeper:
Handlers and failure (official docs) โ
force_handlersand its three ways of being set.