# multi-role organization and re-use

**URL:** <https://forum.ansible.com/t/multi-role-organization-and-re-use/12990>\
**Category:** Ansible Project\
**Created:** [June 19, 2013, 9:16pm UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990 "2013-06-19T21:16:14Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [June 21, 2013, 5:54am UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/21 "2013-06-21T05:54:01Z")

</div>

That’s what I did already. master.yml is in a directory with a group\_vars underneath and I call ansible-playbook from that directory. As a parameter I use “-i /someotherpath/hosts” and in that other path there is also a group\_vars subdirectory. What happens is that the variables from the inventory get executed but not those from the master playbook. From your comment I understood that those from the master playbook should be executed if called from that directory, which I did. So where is the difference coming from?

---

<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, 9:39am UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/22 "2013-06-21T09:39:57Z")

</div>

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

> Already supports relative paths.

Yes, it does. But there is something I don't quite understand. The  
code does this to determine the path of the role directory:

&nbsp;&nbsp;path = utils.path\_dwim(self.basedir, os.path.join('roles', orig\_path))

utils.path\_dwim is a wrapper around os.path.abspath, so we can  
expect an absolut path from this.

self.basedir is the directory containing the playbook.

orig\_path is the name (or path) of the role.

If you set orig\_path to an absolute path, then os.path.join just  
returns that and drops 'roles', and utils.path\_dwim also just  
returns the path.

But if orig\_path is a relative path, then 'roles/' is \*always\*  
prepended.

Therefore, if you want to point to a role directory, you need to use  
absolute paths (which isn't ideal); if you use a relative path, then  
that is always relative to the roles/ subdirectory.

If you want to include a role that is in a subdirectory of the  
playbook, e.g. 'foo/bar', then you need to use '..':

&nbsp;&nbsp;../foo/bar → basedir/roles/../foo/bar → basedir/foo/bar

This is a bit non-intuitive, at least to me.

Would you consider the following patch, which changes the origin for  
the resolution of the relative path to the directory of the  
playbook, which makes more sense to me.

Current users just keep on saying 'foo' to get at 'roles/foo'

But if you say e.g. './foo', then this means './foo', not  
'/roles/foo':

--- a/lib/ansible/playbook/play.py  
+++ b/lib/ansible/playbook/play.py  
@@ -162,7 +162,13 @@ class Play(object):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items = has\_dict.get('with\_items', None)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when = has\_dict.get('when', None)

- path = utils.path\_dwim(self.basedir, os.path.join('roles', orig\_path))  
+ if orig\_path.startswith("."):  
+ # a leading dot makes roles be resolved relative to the  
+ # playbook directory, not a subdirectory 'roles/'  
+ path = utils.path\_dwim(self.basedir, orig\_path)  
+ else:  
+ path = utils.path\_dwim(self.basedir, os.path.join('roles', orig\_path))

---

<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:31pm UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/23 "2013-06-22T17:31:39Z")

</div>

Minor request – when specifying something “failed with an error”, please share the error. Otherwise it just confuses everyone.

In particular, this:

- { role: {{ role\_base\_path }}/role1 }

That’s a YAML syntax error, because you have to quote the string “{{ role\_base\_path }}/role1” since YAML thinks an entry starting with “{” is going to be a hash.

---

<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:34pm UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/24 "2013-06-22T17:34:30Z")

</div>

“Would you consider the following patch, which changes the origin for  
the resolution of the relative path to the directory of the  
playbook, which makes more sense to me.”

Something like that.

I would think instead that if the role contained a path seperator it would look for it in both roles/foo and basedir, so no special syntax about “.” would be required.

I need to test things to see why that may or may not be operational.

---

<div class="post-metadata">

**Author:** ![Chris\_Bennett](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Chris\_Bennett](https://forum.ansible.com/u/Chris_Bennett)\
**Post date:** [June 23, 2013, 12:49am UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/25 "2013-06-23T00:49:59Z")

</div>

> Minor request -- when specifying something "failed with an error", please  
> share the error. Otherwise it just confuses everyone.

I think you are responding to me in another email in this thread.

The errors I saw for different role expressions were:

Playbook:

---

<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 23, 2013, 1:00am UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/26 "2013-06-23T01:00:21Z")

</div>

Thanks!

Right, vars are not loaded prior to loading in roles. You could probably pass this in with “-e role\_base\_dir=path” and it would evaluate.

---

<div class="post-metadata">

**Author:** ![Chris\_Bennett](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Chris\_Bennett](https://forum.ansible.com/u/Chris_Bennett)\
**Post date:** [June 23, 2013, 2:34am UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/27 "2013-06-23T02:34:11Z")

</div>

Thanks for your help & persistence 🙂

> Right, vars are not loaded prior to loading in roles. You could probably  
> pass this in with "-e role\_base\_dir=path" and it would evaluate.

Same outcome:

$ cat site.yml

---

<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 23, 2013, 8:04am UTC](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990/28 "2013-06-23T08:04:47Z")

</div>

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

> I would think instead that if the role contained a path seperator  
> it would look for it in both roles/foo and basedir, so no special  
> syntax about "." would be required.

While at it, would you consider the idea of a list of paths to be  
looked up? I.e. a list that could be extended by the user using  
ansible.cfg?

[Previous page](https://forum.ansible.com/t/multi-role-organization-and-re-use/12990.md?page=1)
