# Referencing paths inside roles

**URL:** <https://forum.ansible.com/t/referencing-paths-inside-roles/12580>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [March 25, 2013, 6:57pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580 "2013-03-25T18:57:54Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [March 25, 2013, 6:57pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/1 "2013-03-25T18:57:54Z")

</div>

The ansible best pracitces content currently is using "../"

I strongly dislike any notion of new syntax, and because your  
playbooks might be run at different levels, some notion of a variable  
named "$base" does not make much sense.

I think "../" is not terrible when needed and strongly preferably to  
introduction of new syntax.

---

<div class="post-metadata">

**Author:** ![nddn](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@nddn](https://forum.ansible.com/u/nddn)\
**Post date:** [March 25, 2013, 7:40pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/2 "2013-03-25T19:40:22Z")

</div>

What do you mean by that?  
If I understand correctly, in included files, paths to file/template sources, which don’t start with / or ~, are resolved relative to parent playbook; so with “…/” you have the same problem I’ve described above.

---

<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:** [March 25, 2013, 7:49pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/3 "2013-03-25T19:49:52Z")

</div>

Yes, if you rearrange files paths have to change. That doesn't seem  
to be terrible though.

Keeping things at the same level and following a common structure  
avoids surprises.

---

<div class="post-metadata">

**Author:** ![nddn](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@nddn](https://forum.ansible.com/u/nddn)\
**Post date:** [March 25, 2013, 8:16pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/4 "2013-03-25T20:16:43Z")

</div>

Yes, it doesn’t seem terrible until you have 100 playbooks in one top dir and you can’t organize them anymore, because all paths will break.  
btw. where can I find those new best practices?

---

<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:** [March 25, 2013, 9:13pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/5 "2013-03-25T21:13:06Z")

</div>

[http://ansible.cc/docs/bestpractices.html](http://ansible.cc/docs/bestpractices.html)

[https://github.com/ansible/ansible-examples](https://github.com/ansible/ansible-examples)

This last repo is just a start, we have a lot more full-stack (or  
should I say, more full use case) examples coming.

---

<div class="post-metadata">

**Author:** ![nddn](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@nddn](https://forum.ansible.com/u/nddn)\
**Post date:** [March 26, 2013, 7:39am UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/6 "2013-03-26T07:39:24Z")

</div>

ok, I don’t really see “…/” as an improvement, it looks even worse, as beside hardcoded role path inside role, now you also have hardcoded parent “roles/” path, and you still depend on the location of the parent playbook.

Anyway, I have another idea, which also fixes this problem, and maybe some others.  
How about including plays inside other play, e.g. playbooks/webservers.yml:

- hosts: webservers  
include:
- …/roles/common/main.yml
- …/roles/php-fpm/main.yml

and roles/php-fpm/main.yml:

- tasks:
- name: php-fpm config  
action: template src=‘templates/www.conf.j2’ dest=‘/etc/php5/fpm/pool.d/www.conf’

This way, roles are just plays which will be merged inside plays in top playbook. You don’t even need separate tasks/handlers directories. For data keys that are set in both plays, parent play can have precedence.

---

<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:** [March 26, 2013, 12:43pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/7 "2013-03-26T12:43:40Z")

</div>

All your parent playbooks should be at the same directory level.

Including plays inside of other plays doesn't make sense.

Playbook includes as they stand today:

- include: play1.yml  
- include: play2.yml

produce a list of plays.

We obviously also have task includes.

You really should keep your master playbooks at the same level.

---

<div class="post-metadata">

**Author:** ![nddn](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@nddn](https://forum.ansible.com/u/nddn)\
**Post date:** [March 26, 2013, 3:47pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/8 "2013-03-26T15:47:54Z")

</div>

I know about current includes, but there are problems with them.  
You can include tasks and handlers in a play and have the above mentioned problem.  
You can include another playbooks inside a playbook, but child playbooks need to have set at least hosts param. Parent playbook can have only include and vars params.  
Yes, you can do something like this:  
 → parent\_playbook:

- include: …/roles/common/common.yml  
vars:  
hosts: webservers
- include: …/roles/php-fpm/main.yml  
vars:  
hosts: webservers

and then inside each role:

- hosts: $hosts  
tasks:  
…

It could work, but again, it’s redundant and ugly.  
And then you later want to, for example, change sudo play param and you need to edit both child and parent playbooks.  
Compare that with example from my previous post.

---

<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:** [March 26, 2013, 10:42pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/9 "2013-03-26T22:42:28Z")

</div>

> You can include another playbooks inside a playbook, but child playbooks  
> need to have set at least hosts param.

Misconception here. You can't include playbooks inside playbooks,  
because it doesn't make sense at all. Playbooks are a list of plays,  
and when you do

- include: blarg  
- play goes here in full  
- include: blarg

What you are doing is making a linear list of 3 plays.

Playbooks do not nest. Nor do task includes. It's always two levels  
deep -- plays then tasks.

---

<div class="post-metadata">

**Author:** ![nddn](https://avatars.discourse-cdn.com/v4/letter/n/8edcca/32.png) [@nddn](https://forum.ansible.com/u/nddn)\
**Post date:** [March 27, 2013, 7:36am UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/10 "2013-03-27T07:36:53Z")

</div>

ok, I was using terminology from your website:  
“Playbooks Including Playbooks  
To further advance the concept of include files, playbook files can include other playbook files.”  
[http://ansible.cc/docs/playbooks2.html#playbooks-including-playbooks](http://ansible.cc/docs/playbooks2.html#playbooks-including-playbooks)

It doesn’t really make a difference if you can have only one play inside included playbook, the same problem still applies.  
My suggestion was not to add more levels of plays, but to merge them, the end result is still two levels. Similar to what you have now, but more flexible.

---

<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:** [March 27, 2013, 12:49pm UTC](https://forum.ansible.com/t/referencing-paths-inside-roles/12580/11 "2013-03-27T12:49:37Z")

</div>

If you wish to have something similar to merging plays together, you  
should look into task includes.
