# Reusing a set of taks across many roles

**URL:** <https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809>\
**Category:** Ansible Project\
**Created:** [November 19, 2015, 2:42pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809 "2015-11-19T14:42:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [November 19, 2015, 2:42pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/1 "2015-11-19T14:42:45Z")

</div>

H All,

I have a piece of code which is required to run by many roles.

Instead of duplicating these tasks which brings maintenance problem as it grows, I would like somehow to re-use same set of tasks across many roles and define them just in one place.

Is there any way to achieve this?

Regards,  
Constantin

---

<div class="post-metadata">

**Author:** ![Markus\_Ellers](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Markus\_Ellers](https://forum.ansible.com/u/Markus_Ellers)\
**Post date:** [November 21, 2015, 6:01pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/2 "2015-11-21T18:01:49Z")

</div>

hi,

you could put these tasks in a separate yml file and include this file whereever you need it.

---

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [November 23, 2015, 3:07pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/3 "2015-11-23T15:07:45Z")

</div>

Thanks Markus,

It worked!

---

<div class="post-metadata">

**Author:** ![rup](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@rup](https://forum.ansible.com/u/rup)\
**Post date:** [December 3, 2015, 5:33pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/4 "2015-12-03T17:33:25Z")

</div>

Where would you “store” such a file? At what point in your directory structure?

---

<div class="post-metadata">

**Author:** ![Markus\_Ellers](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Markus\_Ellers](https://forum.ansible.com/u/Markus_Ellers)\
**Post date:** [December 4, 2015, 12:25pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/5 "2015-12-04T12:25:52Z")

</div>

for ease of use I would store it in the same directory as your other playbooks.  
If you have a bigger / more complex setup I would recommend storing all the “shared” ymls in one location.  
Then make the path to those yml files a variable so you can move it around later without having to edit every include task manually.

---

<div class="post-metadata">

**Author:** ![cosbug](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@cosbug](https://forum.ansible.com/u/cosbug)\
**Post date:** [December 4, 2015, 1:12pm UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/6 "2015-12-04T13:12:12Z")

</div>

I extensively use roles and now came to idea that other roles share some tasks in common.  
What I’ve done so far I’ve split the initial main.yml task file into pieces and use include to re-use these tasks from other roles.  
May be not an elegant solution but it works.

---

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [December 6, 2015, 1:56am UTC](https://forum.ansible.com/t/reusing-a-set-of-taks-across-many-roles/19809/7 "2015-12-06T01:56:50Z")

</div>

You can include role dependencies in the **meta/main.yml** for a role. These are roles that will also be installed by the role, prior to the role installing itself.

So, for common things you need on all machines, you can have a ‘common’ role. Then, in the meta/main.yml for more specific roles, you can establish ‘common’ as a dependency. This is a snippet from one of my more complex roles that combines a lot of smaller roles:

#~ansible/private/slack\_lab/meta/main.yml

# The order listed is the order they will run.

# Tasks for this role (lab) will run after

# tasks for dependencies complete.

dependencies:

- { role: slack\_common }

- { role: slack\_nfsclient }

- { role: slack\_nisclient }

- { role: ntpclient }

- { role: x11forwarding }
