# Roles and playbooks in subdirectories

**URL:** <https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005>\
**Category:** Ansible Project\
**Created:** [June 21, 2013, 2:54pm UTC](https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005 "2013-06-21T14:54:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_f\_krafft](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@martin\_f\_krafft](https://forum.ansible.com/u/martin_f_krafft)\
**Post date:** [June 21, 2013, 2:54pm UTC](https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005/1 "2013-06-21T14:54:43Z")

</div>

Dear list,

one thing that is really nice about Ansible is how the playbook  
becomes the centre of execution, and if you run

&nbsp;&nbsp;ansible-playbook /path/to/my/playbook

then that directory is where Ansible will also look for templates  
required in the tasks of this playbook. Apparently, you can also now  
define group\_vars/host\_vars next to the playbook, so it's a big step  
towards self-containment.

Enter roles, reusable stuff to be used from playbooks. Roles are  
looked for in the subdirectory roles/ in the parent directory of the  
playbook, or you can specify a relative path to the directory of  
another playbook, or you can specify an absolute path to a role  
directory per se.

Now, what is the best approach if I want to use the same role from  
both, playbook1/foo.yml and playbook2/bar.yml?

I'd like to avoid hard-coding absolute paths, as people clone from  
Git to their own locations;

I don't even want to assume that playbook1 and playbook2 are in the  
same place on the filesystem;

And I'd rather not put one copy of a role into playbook1/ and  
another copy into playbook2/.

Is there another way?

If not, I could imagine two solutions:

&nbsp;&nbsp;1. if a role is not found in the usual location, see if a roles/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;directory next to the inventory has it;

&nbsp;&nbsp;2. provide a configuration option and maybe environment variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with which to specify a roles search path. This would be  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;especially useful if distros started providing roles somewhere  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in /usr/share, encouraging people to use them.

The two options can also obviously be combined.

What do you think?

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [June 21, 2013, 4:24pm UTC](https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005/2 "2013-06-21T16:24:10Z")

</div>

Hi,

Another way:

- create symlinks (either for whole roles directory or just specific roles inside it) (it is not nice, but it could get your job done for now)

A configuration option and environment variable t specify a list of paths where to search for roles will probably be needed someday.

I am wondering if it is possible to create a generic path lookup function that could be used all over the place and always behave in the expected way/order. A function that would use the same approach everywhere:

- locate the role: first check if absolute path, than check in “roles” dir relative to playbook (also recurse through all parents), check in “roles” dir relative to inventory file, check in “roles” dir relative to all paths in configuration option or environment variable, as last resort check in “roles” dir relative to current dir, than repeat everything without “roles” dir

- same could be used for locating the module library and similar (replace “roles” with “library” in above)

- same could also be used for locating group\_vars/host\_vars, although only relative to main playbook or inventory file should be encouraged

- locate templates in template module, files in file module or file lookup plugin: first check if absolute path, than if lookup plugin used inside a template check relative to the template (?), than if inside a role check relative to its “templates”/“files” folder, than check in “templates”/“files” folder next to playbooks (recurse parents), check in “templates”/“files” folder next to the inventory file, check in “templates”/“files” relative to all paths in configuration option or environment variable, as last resort check in “templates”/“files” dir relative to current dir

To me it seems that using the same approach everywhere would be possible, or not?

Greetings,  
gw

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 22, 2013, 5:28pm UTC](https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005/3 "2013-06-22T17:28:56Z")

</div>

just path the role where it lies.

---

<div class="post-metadata">

**Author:** ![martin\_f\_krafft](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@martin\_f\_krafft](https://forum.ansible.com/u/martin_f_krafft)\
**Post date:** [June 22, 2013, 5:47pm UTC](https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005/4 "2013-06-22T17:47:37Z")

</div>

also sprach Michael DeHaan \<michael@ansibleworks.com\> [2013.06.22.1928 +0200]:

> just path the role where it lies.

If you are suggesting absolute paths, then how do you propose to do  
team maintenance, where user 'bob' checks out to /home/bob/ansible  
and user 'alice' has her stuff in  
/srv/users/a/alice/sysadmin/ansible/site1 ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 22, 2013, 6:13pm UTC](https://forum.ansible.com/t/roles-and-playbooks-in-subdirectories/13005/5 "2013-06-22T18:13:59Z")

</div>

I am not suggesting absolute paths.
