We are currently asking ourselves in Team in which order handlers are executed from roles. We have several roles in a playbook with several handlers. In some playbooks, the handlers are executed in alphabetical order of the roles, but not in all playbooks.
They are executed based on the order they are defined in. Here’s a relevant snippet from the documentation:
In the above example, the handlers are executed on task change in the following order: Restart memcached, Restart apache. Handlers are executed in the order they are defined in the handlers section, not in the order listed in the notify statement.
Can you include the relevant handlers files so I can see the order they are defined in to try to reproduce the issue? Can you share the version of ansible-core you’re using too?
The handlers file in haproxy defines the handlers in this order:
Haproxy Daemon reload
Haproxy started
Haproxy restarted
Haproxy reloaded
The result you shared matches the order, I think.
I’m not sure where the handlers are notified, or if this could be a point of misunderstanding, but the notify order is not the definition-order. Here’s the relevant bit from the documentation:
Handlers are executed in the order they are defined in the handlers section, not in the order listed in the notify statement.
If I’m missing something, could you share the expected result?
First, handlers, no matter where defined, are attached to the play, so while a role can provide handlers they are always PLAY level handlers. The order of execution depends on the order in which they were added/defined to the play. This can get tricky with dependent roles and conflicting names, but should still hold true.
We have no dependency’s in our Roles. If I understand you correctly, all (handlers in the) roles should now be executed in the same order as listed under “roles:”, but currently they are executed in alphabetical order.
Is it possible to debug the order of handlers?
We dont want to flush the handlers at the end of each Role, just to ensure they get executed in the right order.
I created a simple reproducer, but everything runs just fine - no Bug!
Now we have to test our Ansible Setup and all used Versions for the cause of the problem