# common file dependencies for roles

**URL:** <https://forum.ansible.com/t/common-file-dependencies-for-roles/20124>\
**Category:** Ansible Project\
**Created:** [January 5, 2016, 1:32am UTC](https://forum.ansible.com/t/common-file-dependencies-for-roles/20124 "2016-01-05T01:32:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshi\_Spendiff1](https://avatars.discourse-cdn.com/v4/letter/y/8c91f0/32.png) [@Yoshi\_Spendiff1](https://forum.ansible.com/u/Yoshi_Spendiff1)\
**Post date:** [January 5, 2016, 1:32am UTC](https://forum.ansible.com/t/common-file-dependencies-for-roles/20124/1 "2016-01-05T01:32:30Z")

</div>

Hi,

I’m wondering if anyone knows of any tricks for neatly handling common file dependencies for roles. I initially thought having a common dependent role in meta/main.yml would facilitate this but apparently dependencies don’t apply to files (which does make sense if roles are though of as totally isolated), but is there some way to achieve this? Or is there some way to specify search paths for files inside a role or something?

So far the best I’ve come up with is putting a _files_ and/or _templates_ directory in the root of the ansible directory and then referencing files/templates with _src=file/myfile.txt_ and/or _src=templates/mytemplate.txt_ (I’ve also seen the recommendation to use _src={{ inventory\_dir }}/files/myfile.txt_ but that doesn’t seem necessary). The problem with this is that it doesn’t really organise dependencies for roles but rather just lumps everything into one directory and makes it difficult to share roles externally.

Alternatively, is there some way to specify multiple task files in a role so that all other components can be shared, i.e. something like the following (or Chef cookbook recipes really)

- name: myplay  
roles:
- myrole.create (roles/myrole/tasks/create.yml)
- myrole (roles/myrole/tasks/main.yml)
- myrole.delete (roles/myrole/tasks/delete.yml)

Regards,  
Yoshi

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [January 5, 2016, 10:44am UTC](https://forum.ansible.com/t/common-file-dependencies-for-roles/20124/2 "2016-01-05T10:44:35Z")

</div>

Hi,

I would try to get paths for files into variables in meta roles and export it as facts:

defaults/main.yml:  
rolename\_path: {{ role\_path }}

tasks/main.yml:

- set\_fact: rolename\_path={{ rolename\_path }}

And that use this variable to get file:  
{{ rolepath\_name + ‘files/filename.txt’ }}

Could work.

---

<div class="post-metadata">

**Author:** ![Yoshi\_Spendiff](https://avatars.discourse-cdn.com/v4/letter/y/6f9a4e/32.png) [@Yoshi\_Spendiff](https://forum.ansible.com/u/Yoshi_Spendiff)\
**Post date:** [January 5, 2016, 5:36pm UTC](https://forum.ansible.com/t/common-file-dependencies-for-roles/20124/3 "2016-01-05T17:36:23Z")

</div>

Thanks David,

That’s quite a reasonable solution actually, for one it works but also it makes you consider naming conventions for the facts and dependencies that will use it.

I had to quote the {{ role\_path }} in defaults/main.yml for it to work for me.

So variables defined in defaults/main.yml are scoped only for the execution of tasks in that role?

Regards,  
Yoshi

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [January 6, 2016, 10:37am UTC](https://forum.ansible.com/t/common-file-dependencies-for-roles/20124/4 "2016-01-06T10:37:48Z")

</div>

Hi Yoshi,

role variables are accessible only in roles, thats why create a fact that is accessible to host (and thru hostvars from other hosts if needed). Personally I always scope role variables with role name, easier for me to read host\_vars/ group\_vars files.

---

<div class="post-metadata">

**Author:** ![Yoshi\_Spendiff](https://avatars.discourse-cdn.com/v4/letter/y/6f9a4e/32.png) [@Yoshi\_Spendiff](https://forum.ansible.com/u/Yoshi_Spendiff)\
**Post date:** [January 6, 2016, 4:46pm UTC](https://forum.ansible.com/t/common-file-dependencies-for-roles/20124/5 "2016-01-06T16:46:12Z")

</div>

Many thanks
