Controlling when handlers run
By default, handlers run after all the tasks in a particular play have been completed. Notified handlers are executed automatically after each of the following sections, in the following order:pre_tasks,roles/tasksandpost_tasks. This approach is efficient, because the handler only runs once, regardless of how many tasks notify it. For example, if multiple tasks update a configuration file and notify a handler to restart Apache, Ansible only bounces Apache once to avoid unnecessary restarts.
If you need handlers to run before the end of the play, add a task to flush them using themeta module, which executes Ansible actions:
Themeta:flush_handlerstask triggers any handlers that have been notified at that point in the play.
Once handlers are executed, either automatically after each mentioned section or manually by theflush_handlersmeta task, they can be notified and run again in later sections of the play.