# \[Ansible role\] action level reusability and order

**URL:** <https://forum.ansible.com/t/ansible-role-action-level-reusability-and-order/20252>\
**Category:** Ansible Project\
**Created:** [January 18, 2016, 12:44pm UTC](https://forum.ansible.com/t/ansible-role-action-level-reusability-and-order/20252 "2016-01-18T12:44:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wukang\_Lin](https://avatars.discourse-cdn.com/v4/letter/w/5daacb/32.png) [@Wukang\_Lin](https://forum.ansible.com/u/Wukang_Lin)\
**Post date:** [January 18, 2016, 12:44pm UTC](https://forum.ansible.com/t/ansible-role-action-level-reusability-and-order/20252/1 "2016-01-18T12:44:05Z")

</div>

Hi,  
all. I have review some similar topic on task reusability of ansible role and task execute order. But i still cannot get an answer to my problem. here it is:  
suppose i have a role named ‘service\_a’, and I have write a set of tasks in ‘service\_a/tasks/tasks.yml’, such like this:

# service\_a/tasks/tasks.yml

- name: open the door  
…
- name: get mails from mailbox  
…
- name: turn on the light  
…

Now, I want to organize those actions in different orders, as below (just some fake code to explain my needs):

# service\_a/tasks/main.yml

- name: get mails then open the door, because mail box is outside the room  
tasks:

- “get mails from mailbox”

- “open the door”

- “turn on the light”  
…  
tags: mailbox\_outside\_the\_room

- name: open the door then get mails, because mail box is inside the room  
tasks:

- “open the door”

- “turn on the light”

- “get mails from mailbox”

…  
tags: mailbox\_inside\_the\_room

How to implements like this? Can any Help?

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [January 18, 2016, 2:22pm UTC](https://forum.ansible.com/t/ansible-role-action-level-reusability-and-order/20252/2 "2016-01-18T14:22:13Z")

</div>

Hi,

it is actually not possible on task level, Ansible is going thru them from top to bottom. But there can be some possibilities how to do it:

- (maybe) try to think about your own Execution strategy: [http://docs.ansible.com/ansible/playbooks\_strategies.html](http://docs.ansible.com/ansible/playbooks_strategies.html) but really do not know, if it is possible by them

- you can use handlers, defne not a tasks but handlers, than do:

- name: open the door then get mails, because mail box is inside the room  
debug: msg=“calling handlers to do a job”  
changed\_when: true  
notify:

- “open the door”

- “turn on the light”

- “get mails from mailbox”

- meta: flush\_handlers

Handlers are evaliuated from top to bottom too, so you can use different sorting.

But, to be honest, it does not look like somethink that can be easilly done in Ansible. Maybe try to describe your problem, what you try to solve (not a solution you try to implement, but description of your problem in more general way).

David
