# Handler: In which order are Role Handlers executed?

**URL:** <https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399>\
**Category:** Get Help\
**Created:** [September 10, 2024, 1:23pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399 "2024-09-10T13:23:11Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 10, 2024, 1:23pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/1 "2024-09-10T13:23:11Z")

</div>

Hi,

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.

We can’t find anything in the documentation either: [Handlers: running operations on change — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_handlers.html#notifying-handlers)

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 10, 2024, 1:30pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/2 "2024-09-10T13:30:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 11, 2024, 7:17am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/3 "2024-09-11T07:17:47Z")

</div>

This is the Roles Part of our Playbook:

```auto
roles:
    - role: elasticsearch-apikey
    - role: haproxy
    - role: executable-jar

```

This is the Result:

```auto
RUNNING HANDLER [elasticsearch-apikey : esapikey_invalidate_keys] **************
Tuesday 30 July 2024 17:04:14 +0200 (0:00:01.281) 0:00:34.533 ********** 
ok: [server11-dev]

RUNNING HANDLER [executable-jar : Executable_jar is restarted] *****************
Tuesday 30 July 2024 17:04:16 +0200 (0:00:01.590) 0:00:36.123 ********** 
changed: [server11-dev]

RUNNING HANDLER [executable-jar : Executable_jar is alive] *********************
Tuesday 30 July 2024 17:04:16 +0200 (0:00:00.518) 0:00:36.641 ********** 
ok: [server11-dev]

RUNNING HANDLER [haproxy : Haproxy Daemon reload] ******************************
Tuesday 30 July 2024 17:04:23 +0200 (0:00:07.031) 0:00:43.673 ********** 
ok: [server11-dev]

RUNNING HANDLER [haproxy : Haproxy started] ************************************
Tuesday 30 July 2024 17:04:24 +0200 (0:00:00.850) 0:00:44.523 ********** 
ok: [server11-dev]

RUNNING HANDLER [haproxy : Haproxy restarted] **********************************
Tuesday 30 July 2024 17:04:25 +0200 (0:00:00.499) 0:00:45.023 ********** 
changed: [server11-dev]

RUNNING HANDLER [haproxy : Haproxy reloaded] ***********************************
Tuesday 30 July 2024 17:04:26 +0200 (0:00:00.946) 0:00:45.970 ********** 
changed: [server11-dev]

```

But this is not the Order we defined!

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 11, 2024, 11:45am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/4 "2024-09-11T11:45:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 11, 2024, 2:46pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/5 "2024-09-11T14:46:21Z")

</div>

/roles/elasticsearch-apikey/handlers/main.yml:

```yaml
- name: esapikey_invalidate_keys
  ansible.builtin.uri:
    url: "{{ esapikey_url }}/_security/api_key"

```

/roles/executable-jar/handlers/main.yml:

```yaml
- name: Executable_jar is restarted
  ansible.builtin.service:
    name: executable-jar
    state: restarted
    enabled: true
  become: true
  listen: executable_jar_restarted

- name: Executable_jar is alive
  ansible.builtin.uri:
    url: "{{ executable_jar_alive_url }}"
    status_code: 200
  register: result
  until: result.status == 200
  retries: 60
  delay: 1
  tags: config
  listen: executable_jar_alive
  when:
    - not ansible_check_mode
    - executable_jar_alive_check

```

/roles/haproxy/handlers/main.yml:

```auto
- name: Haproxy Daemon reload
  ansible.builtin.systemd:
    daemon_reload: true
  become: true
  listen: Haproxy_daemon_reload

- name: Haproxy started
  ansible.builtin.service:
    name: haproxy
    state: started
    enabled: true
  become: true
  listen: Haproxy_started

- name: Haproxy restarted
  ansible.builtin.service:
    name: haproxy
    state: restarted
    enabled: true
  become: true
  listen: Haproxy_restarted

- name: Haproxy reloaded
  ansible.builtin.service:
    name: haproxy
    state: reloaded
    enabled: true
  become: true
  listen: Haproxy_reloaded

```

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 11, 2024, 3:01pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/6 "2024-09-11T15:01:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 11, 2024, 3:11pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/7 "2024-09-11T15:11:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 13, 2024, 7:36am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/8 "2024-09-13T07:36:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 13, 2024, 7:41am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/9 "2024-09-13T07:41:22Z")

</div>

We have 3 Roles with Handlers:

```auto
roles:
    - role: Role_A
    - role: Role_C
    - role: Role_B

```

and I expect these execution Order of the handlers:

```auto
- Role_A_Handler_1
- Role_A_Handler_2
- Role_C_Handler_1
- Role_C_Handler_2
- Role_B_Handler_1
- Role_B_Handler_2

```

but I get:

```auto
- Role_A_Handler_1
- Role_A_Handler_2
- Role_B_Handler_1
- Role_B_Handler_2
- Role_C_Handler_1
- Role_C_Handler_2

```

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [September 13, 2024, 10:10am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/10 "2024-09-13T10:10:12Z")

</div>

And what is the order of handler notifies? Same as role order? Do you notify a handler in one role from some other role?

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 13, 2024, 10:40am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/11 "2024-09-13T10:40:37Z")

</div>

> [@bvitnik](#):
>
> Do you notify a handler in one role from some other role?

No, we have no cross roles notify. All handler get notified in there own Role.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 13, 2024, 2:11pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/12 "2024-09-13T14:11:11Z")

</div>

Notification should not influence execution order, if the above is true, this is a bug. I would submit to github with a simple reproducer.

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 23, 2024, 8:19am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/13 "2024-09-23T08:19:28Z")

</div>

I created a simple [reproducer](https://github.com/RayJin2000/ansible-handler-execution-order), 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 😑

---

<div class="post-metadata">

**Author:** ![RayJin2000](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rayjin2000/32/3641_2.png) [@RayJin2000](https://forum.ansible.com/u/RayJin2000)\
**Post date:** [September 23, 2024, 8:48am UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/14 "2024-09-23T08:48:10Z")

</div>

I found our Problem. We have used the same Role more than once in the same Playbook.

```yaml
  roles:
    - role: role_2
    - role: role_1
    - role: role_3
    - role: role_1

```

Handler Executon Order:

- role\_2
- role\_3
- role\_1

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 23, 2024, 2:52pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/15 "2024-09-23T14:52:16Z")

</div>

So it works as expected, role1 handlers get ‘redefined’ so the first definition is masked by the latter, accounting for the change in order.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 23, 2024, 2:52pm UTC](https://forum.ansible.com/t/handler-in-which-order-are-role-handlers-executed/8399/16 "2024-10-23T14:52:49Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
