# Execution order of "import\_role" and "include\_role"?

**URL:** <https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232>\
**Category:** Get Help\
**Tags:** playbook, documentation\
**Created:** [November 29, 2024, 4:51pm UTC](https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232 "2024-11-29T16:51:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Toni](https://forum.ansible.com/u/Toni)\
**Post date:** [November 29, 2024, 4:51pm UTC](https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232/1 "2024-11-29T16:51:34Z")

</div>

Hi,

I have an Ansible role where I want to run another Ansible role much like a subroutine. So, in the middle of a playbook (tasks/main.yaml) I have an “import\_role” task to run that other role. I used to have Ansible 2.9 or so, where this worked nicely, but recently upgraded Ansible to 2.18.0, and now, the imported role gets run at the start of my playbook, ie, at the start of my role, which fails everytime because the environment is not yet properly set up at that point - there’s a reason why I don’t just “depend” on the other role via meta.yml. Did I miss something, or how can I make the role to execute in order, not at the beginning? I also tried “include\_role”, but that didn’t help, either.

What gives?

---

<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:** [November 29, 2024, 7:21pm UTC](https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232/2 "2024-11-29T19:21:44Z")

</div>

While imports do get executed at ‘compile’ time, the tasks for the role should only execute in the order they appear in the play. So aside from the import/include itself running at different times, the tasks included/imported should run as they are placed. There have been changes to imports and includes in many versions, but the task execution order should not have changed. Can you provide an example/simple reproducer?

---

<div class="post-metadata">

**Author:** ![Toni](https://avatars.discourse-cdn.com/v4/letter/t/8797f3/32.png) [@Toni](https://forum.ansible.com/u/Toni)\
**Post date:** [November 29, 2024, 8:50pm UTC](https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232/3 "2024-11-29T20:50:25Z")

</div>

I’ve just tried to reproduce the situation with two brand-new roles, and it worked as it should. I also found my problem with the other role: I had mentioned the imported role as a dependency in the meta.yml. 🤦‍♂️ Sorry for the noise.

---

<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:** [November 29, 2024, 8:56pm UTC](https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232/4 "2024-11-29T20:56:14Z")

</div>

No worries, there is a reason my avatar is a rubber ducky!

---

<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:** [December 30, 2024, 10:26am UTC](https://forum.ansible.com/t/execution-order-of-import-role-and-include-role/11232/6 "2024-12-30T10:26:17Z")

</div>

Closed on December 30, 2024, 10:26am UTC

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