# Role directory path variable

**URL:** <https://forum.ansible.com/t/role-directory-path-variable/22010>\
**Category:** Ansible Project\
**Created:** [July 13, 2016, 9:09pm UTC](https://forum.ansible.com/t/role-directory-path-variable/22010 "2016-07-13T21:09:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://avatars.discourse-cdn.com/v4/letter/n/c37758/32.png) [@neubyr](https://forum.ansible.com/u/neubyr)\
**Post date:** [July 13, 2016, 9:09pm UTC](https://forum.ansible.com/t/role-directory-path-variable/22010/1 "2016-07-13T21:09:17Z")

</div>

I am wondering if there is any recommended approach for passing filename paths across roles.

As an example, take a look at [https://bitbucket.org/neubyr/ansible-example/src](https://bitbucket.org/neubyr/ansible-example/src) repository. There are two roles myapp and webserver.

- webserver role configures webserver and accepts custom configuration file as an input parameter. It will install config file and do some other tasks.
- myapp role configures application and one of it’s step is webserver configuration. It will pass webserver configuration file path to the webserver role so that it can handle that part.

Both roles are tied together in playbook test.yml. Right now file name passed to the webserver role is relative path from the playbook file. Is there any way to make this path independent of playbook location and reference it using role directory? I’ve tried using role\_path variable in role dependencies meta.yml file, but role\_path is evaluated after calling dependency role so it is not helping either.

Is there any way to provide role directory variable? Or Is there any way to pass dependency calling role’s path?

- thanks, N

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 14, 2016, 2:10pm UTC](https://forum.ansible.com/t/role-directory-path-variable/22010/2 "2016-07-14T14:10:47Z")

</div>

Yes - if you have tasks in a role 'thingy' like this:

- name: config  
&nbsp;&nbsp;copy: src=etc/file.conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest=/etc/file.conf

- name: more config  
&nbsp;&nbsp;&nbsp;template: src=etc/file2.conf.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest=/etc/file2.conf

then

\* the copy task will load its source file from roles/thingy/files/etc/file.conf  
\* the template task will load its template from  
roles/thingy/templates/etc/file2.conf.j2

---

<div class="post-metadata">

**Author:** ![neubyr](https://avatars.discourse-cdn.com/v4/letter/n/c37758/32.png) [@neubyr](https://forum.ansible.com/u/neubyr)\
**Post date:** [July 14, 2016, 8:19pm UTC](https://forum.ansible.com/t/role-directory-path-variable/22010/3 "2016-07-14T20:19:01Z")

</div>

Thank you for the details. I am wondering how to pass file name in one role to another role as variable parameter.

As mentioned in previous post, take example of configuring webserver for a particular application. webserver setup remains same for multiple applications except configuration file. So if we develop a webserver role that accepts file path parameter, then same webserver role could be reused by other application roles. Now it makes sense to keep application specific webserver configuration file inside application role and pass it’s path to webserver role. This helps in keeping all application specific configuration files inside a single role.

If Ansible had some roles path variable or special path ‘role’, then it would make above pattern easy. So we could say something like src=ROLES/myapp/files/testfile .

It seems like that is not possible with Ansible.

- N

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 14, 2016, 9:17pm UTC](https://forum.ansible.com/t/role-directory-path-variable/22010/4 "2016-07-14T21:17:44Z")

</div>

Right, let me check I get the requirement -  
so for example we're talking about say an 'apache' role and a 'kibana'  
role that's going to need a vhost setup.

(with ansible 1.9 at least), if I set a vhost\_conf\_dir var in my  
apache role (which is a path to a directory you can drop .vhost files  
into to have them auto-included, nginx etc. has a similar feature) and  
then have my kibana role list 'apache' as a dependency in its  
meta/main.yml, it will be able to see that var and use it.

Another way is to explicitly set a 'global' (play wide or host wide,  
point is its set external to the roles ) vhost\_conf\_dir and have each  
role use it (that's less 'magical' so personally I prefer it, it's  
easier to see what's going on).

One downside is the 'kibana' role needs handlers to restart the  
webserver, which is a bit ugly.

Either way, you're going to have 2 roles that are co-dependant and not  
too re-usable. That would  
still be the case if you had the exact feature you describe, of course.

I'm of the opinion that 'role reuse' is a wild goose chase in CM  
systems, so I'm fine with that 🙂

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 14, 2016, 9:20pm UTC](https://forum.ansible.com/t/role-directory-path-variable/22010/5 "2016-07-14T21:20:04Z")

</div>

Sorry to reply to myself, but I checked and (with the meta pattern on  
1.9.x at least), the kibana role \_will\_ be  
able to trigger the handlers defined in the apache role. Which is nice.
