# Role lookup

**URL:** <https://forum.ansible.com/t/role-lookup/14344>\
**Category:** Ansible Developer\
**Created:** [January 15, 2014, 1:32am UTC](https://forum.ansible.com/t/role-lookup/14344 "2014-01-15T01:32:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [January 15, 2014, 1:32am UTC](https://forum.ansible.com/t/role-lookup/14344/1 "2014-01-15T01:32:22Z")

</div>

tl;dr - Is there a reason that roles are looked for relative to the current location?

We have a site.yml playbook that includes a bunch of other playbooks (one for each role), but this clutters our root directory. I was hoping that I could just move the playbooks to a “playbooks” directory and then include them from site.yml with their new path. However, the include seems to move the cwd to the playbooks directory instead of being at the root. Thusly, the roles can no longer be found.

Is there a good reason for roles specified in playbooks to not be looked up relative to the root or at least the cwd of the ansible-playbook invocation?

---

<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:** [January 15, 2014, 2:07am UTC](https://forum.ansible.com/t/role-lookup/14344/2 "2014-01-15T02:07:20Z")

</div>

Shouldn’t be the case.

Roles are looked for relative to the playbook basedir, which shouldn’t require cluttering up your root directory at all.

The roles path is also configurable in ansible.cfg as of 1.4

I don’t think it’s appropriate to consider your current cwd, as you might be sitting around in /tmp, or /root, or anywhere, relative to the playbook is a good default, if not, it’s definitely configurable in ansible.cfg.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [January 15, 2014, 2:19am UTC](https://forum.ansible.com/t/role-lookup/14344/3 "2014-01-15T02:19:54Z")

</div>

Okay, so maybe this is a bug then. I’ve put together a toy and get the output shown:  
[http://pastebin.com/ir1C1VNQ](http://pastebin.com/ir1C1VNQ)

If this is not the expected output, I’ll file a bug. Thanks.

ansible-playbook 1.5 (devel 332d3d6a0e) last updated 2014/01/14 18:18:47 (GMT -700)

---

<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:** [January 15, 2014, 2:35am UTC](https://forum.ansible.com/t/role-lookup/14344/4 "2014-01-15T02:35:36Z")

</div>

It’s relative to your top level playbook, so if site.yml isn’t in playbooks that would explain it (i.e. not a bug), and you can just move roles up a level.

site.yml  
playbooks/\*  
roles/\*

etc.

Though otherwise you should probably configure your roles path, which is also helpful when working with, say, Galaxy, like have your roles regularly checked out in /opt/roles or /serv/roles, etc.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [January 15, 2014, 2:54am UTC](https://forum.ansible.com/t/role-lookup/14344/5 "2014-01-15T02:54:12Z")

</div>

> It’s relative to your top level playbook, so if site.yml isn’t in playbooks that would explain it (i.e. not a bug), and you can just move roles up a level.
> 
> site.yml  
> playbooks/\*  
> roles/\*
> 
> etc.

This is exactly what we have. If I am following correctly, what I am doing should work, correct?

---

<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:** [January 15, 2014, 3:03am UTC](https://forum.ansible.com/t/role-lookup/14344/6 "2014-01-15T03:03:27Z")

</div>

It appears I wasn’t remembering the code correctly.

Just ran a test of this and it does look like it’s looking relative to the playbook files that get included.

That being said, we must not change this behavior, but it should probably also look in the location of site.yml

Our standard practice is to actually have all the playbooks at top level and then have a roles subdirectory.

I don’t consider this a bug more so “the way it works”, but I’m not against it looking in additional locations.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [January 15, 2014, 3:11am UTC](https://forum.ansible.com/t/role-lookup/14344/7 "2014-01-15T03:11:08Z")

</div>

Cool. I just wanted to determine bug/not bug. I’ll submit a feature request for this.

Thanks.

---

<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:** [January 15, 2014, 3:13am UTC](https://forum.ansible.com/t/role-lookup/14344/8 "2014-01-15T03:13:41Z")

</div>

For the record, the reason it looks relative to the included file is such that you can have a top level site.yml and subfiles in a playbooks subdirectory, and it can still find them.

In your proposal, where you have site.yml at the TOP level, and playbooks in a subdir, if you invoked site.yml, and it only looked relative to the first, it wouldn’t find them.

Thus, you can technically do “playbooks/roles/\*” now, and will be totally happy, provided you have nothing in that top level playbook looking for things.

I think I’d still recommend roles path in that case.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [January 15, 2014, 3:21am UTC](https://forum.ansible.com/t/role-lookup/14344/9 "2014-01-15T03:21:52Z")

</div>

I guess while we are on the topic, can you explain why an include directive doesn’t just include the file contents within the current scope? I suppose, however, that Python works the way ansible does.

---

<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:** [January 15, 2014, 3:48am UTC](https://forum.ansible.com/t/role-lookup/14344/10 "2014-01-15T03:48:36Z")

</div>

“can you explain why an include directive doesn’t just include the file contents within the current scope?”

IMHO there’s nothing scope related here per se, i.e. variable related, but rather just a question of whether ansible moves the basedir.

And we’re using the basedir of the included file so that relative paths work.

What the above is proposing is that roles path uses a bit more additionally than just that.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [January 15, 2014, 9:23am UTC](https://forum.ansible.com/t/role-lookup/14344/11 "2014-01-15T09:23:02Z")

</div>

Okay. That makes sense. Thanks.
