I'm running Ansible 2.0, and I could just run this, but I could also be tricked in to believing something that isn't true by my empirical tests and I can find no documentation to tell me when handlers are supposed to be ran.
If handlers aren't ran at the end of their tasks, this is my conundrum. I've got a playbook with 5 roles in it, I want to add a 6 role to the end that needs to have the handlers of the 4th role completed before it can start.
Is there any way to run Ansible to rely on a handler being completed (i.e. a role being completely completed) before doing something else or am I using handlers wrong?
Handlers are executed:
meta: flush_handlers
taskSo "to add a 6 role to the end that needs to have the handlers of the 4th role" you need:
or add a meta task and include the 6th role with
include_role
module:For your use case, I'd suggest the first method as the
include_role
module is still very fresh and there are quirks when using it (see this question on SO).Moreover, please notice that handlers' names and listen calls are global, so two handlers in separate roles will be in conflict if they had the same name and both roles were assigned in a single play. (ref. Handlers: Running Operations On Change)
Empirical proof (run this shell script to confirm handlers are executed at the end of the play - there were contradicting comments and answers here):
Result:
Play modified to contain
meta: flush_handlers
:The result:
1) Handlers that do the same thing should be named the same.
restart nginx
ALWAYS restarts nginx, nothandler1
andhandler2
2) Handlers are run at the END of the entire "Play" a play scoped to your sections.
3) I would use the
register
andwhen
functions for tasks that should be restarted, note this var should carry with you.Code Source
Lots of ways to do the same task. Handlers were designed to prevent restarting the same process multiple times, such as multiple changes to a nginx server that has websites, ssl certs, and other tasks that need service restarts.