# What is the order of role-specific vs. imported handlers and can it be changed?

**URL:** <https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492>\
**Category:** Get Help\
**Created:** [September 13, 2024, 1:40pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492 "2024-09-13T13:40:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![drosera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/drosera/32/4737_2.png) [@drosera](https://forum.ansible.com/u/drosera)\
**Post date:** [September 13, 2024, 1:40pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/1 "2024-09-13T13:40:29Z")

</div>

Hello,

our handlers are mostly specified in the role itself (like `roles/server/handlers/main.yml`), but _some_ handlers, which are required in many roles, are specified in a central “library” role (`roles/library/handlers/main.yml`) and imported into the main playbook (`site.yml`) in the block, which also runs the role like this

```auto
- hosts: all_servers
  name: "====== SERVER CONFIGURATION ======"
  roles:
    - server
  handlers:
    - ansible.builtin.import_tasks: roles/library/handlers/main.yml

```

**It now seems, that the imported handlers are placed _after_ the role-specific handlers** , which breaks some plays, where the library handlers should run _before_ the role-specific handlers, but they run _after_ them (or rather, the play fails, as the handlers didn’t run).

We are aware, that handlers are executed in the order in which they are written in the code, not how they have been called.

We have two questions regarding this situation:

1. Can it be confirmed, that imported handlers are attached _after_ the list of role-specific handlers?
2. Is there a way to load imported handlers _before_ the role-specific handlers?

Thanks in advance

---

<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, 11:25pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/2 "2024-09-13T23:25:00Z")

</div>

I’m not sure about the first question but I can give you the answer for the second.

You can make your `library` role to be a dummy role by making an empty `tasks/main.yml`. After that, you can call the role together with other roles like so:

```yaml
roles:
  - library
  - server

```

This way handlers from `library` role will be imported into play before all other role handlers and no additional tasks will be executed since `library` role is empty. You can also define variable defaults in `library` role which will also be imported without running any tasks.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 14, 2024, 3:12am UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/3 "2024-09-14T03:12:11Z")

</div>

> [@bvitnik](#):
>
> You can make your `library` role to be a dummy role by making an empty `tasks/main.yml`.

This is not necessary. Roles are not required to contain tasks.

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [September 17, 2024, 6:23pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/4 "2024-09-17T18:23:57Z")

</div>

Hi.  
I think a better approach would be to add the handler from the central library role to each respective role. It might be a “little” more work, but this ensures that your roles can function, and are easily shareable, without depending on a “central” role.

---

<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 17, 2024, 6:28pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/5 "2024-09-17T18:28:56Z")

</div>

Yes but then when you update the handler code you have to update it in multiple places. With a complex code this quickly leads to high maintenance overhead.

I think the OP presented here a simple example but has much more in mind hence a role that is called a “library” (something complex and shared).

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [September 17, 2024, 6:42pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/6 "2024-09-17T18:42:20Z")

</div>

Yes, it would be more work, as you have to copy the handler to all the roles that use it, but I believe creating a dependency to a central role causes more complexity 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![drosera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/drosera/32/4737_2.png) [@drosera](https://forum.ansible.com/u/drosera)\
**Post date:** [September 20, 2024, 8:35pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/7 "2024-09-20T20:35:54Z")

</div>

Dear @bvitnik , @flowerysong, thanks for your ideas and suggestions. I clearly want to stay with the centrally managed handlers and not add code redundancy to our Ansible setup. IMHO this is generally _much_ easier to maintain, even though it might come with some small additional complexity 🙂. Our central “library” role contains anyway much more than just handlers (like parametrizable generic task “templates” ecc.).

@bvitnik’s idea of importing the library as a role is interesting. As a variant of this, I am now importing the library handler as first task in the role’s handler file (`roles/xxx/handlers.main.yml`)

```auto
---
- name: "Import library handlers"
  ansible.builtin.import_tasks: roles/library/handlers/main.yml

- name: ....

```

This ensures the right order. I’m still considering adding the complete role as suggested by @bvitnik, though.

---

<div class="post-metadata">

**Author:** ![drosera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/drosera/32/4737_2.png) [@drosera](https://forum.ansible.com/u/drosera)\
**Post date:** [September 20, 2024, 8:37pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/8 "2024-09-20T20:37:25Z")

</div>

Also thanks to @jholland of course (the forum software didn’t allow me to mention more than two users in the post above 😄)

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 20, 2024, 9:33pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/9 "2024-09-20T21:33:19Z")

</div>

> [@drosera](#):
>
> ` ansible.builtin.import_tasks: roles/library/handlers/main.yml`

You should not access things that are inside of other roles using non-role imports. It confuses people reading your code and defeats the purpose of putting it in a role.

If you want to import a loose task file using `import_tasks`, don’t put it in a role. If you want to share handlers using a role, use one of the supported methods for calling that role.

---

<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 20, 2024, 9:33pm UTC](https://forum.ansible.com/t/what-is-the-order-of-role-specific-vs-imported-handlers-and-can-it-be-changed/8492/10 "2024-10-20T21:33:32Z")

</div>

Closed on October 20, 2024, 9:33pm UTC

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