# Organizing files in subdirectories  at role-level

**URL:** <https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583>\
**Category:** Ansible Project\
**Created:** [September 23, 2017, 2:23pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583 "2017-09-23T14:23:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torsten\_Crass](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@Torsten\_Crass](https://forum.ansible.com/u/Torsten_Crass)\
**Post date:** [September 23, 2017, 2:23pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583/1 "2017-09-23T14:23:30Z")

</div>

Hi there,

I wonder how (if at all) it was possible to refer to files in subdirectories of a role’s “file” folder when using modules like “copy” and the like.

To clarify, let’s assume we have a directory layout like this:

root/

- main.yaml
- files/
- foo.txt
- bar/
- baz.txt
- roles/
- mumbo/
- files/
- foo.txt
- bar/
- baz.txt
- tasks/
- main.yaml

Now within root/main.yaml, I can refer to root/files/bar/baz.txt like

- name: copy bar/baz.txt to host  
copy:
- src: files/bar/baz.txt
- dest: /some/path/baz.txt

When, however, I include the “mumbo” role in root/main.yaml and I try to refer to root/roles/mumbo/files/bar/baz.txt from within the root/roles/mumbo/tasks/main.yaml file in a similar fashion, i.e. like

- name: copy bar/baz.txt to mumbo host  
copy:
- src: files/bar/baz.txt
- dest: /some/other/path/baz.txt

or

- name: copy bar/baz.txt to mumbo host  
copy:
- src: …/files/bar/baz.txt
- dest: /some/other/path/baz.txt

ansible complains that it is unable to locate the source file. It is possible, though, to refer to files residing directly in the root/roles/mumbo/files folder:

- name: copy foo.txt to mumbo host  
copy:
- src: foo.txt
- dest: /yet/another/path/foo.txt

works like a charm.

So am I missing something or is it really not possible to organize files in subdirectories?

Cheers –

Torsten

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 23, 2017, 3:28pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583/2 "2017-09-23T15:28:28Z")

</div>

Roles in Ansible has the concept of files, so copy will automatically be looking in files, so just use

src: bar/baz.txt

---

<div class="post-metadata">

**Author:** ![Torsten\_Crass](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@Torsten\_Crass](https://forum.ansible.com/u/Torsten_Crass)\
**Post date:** [September 23, 2017, 3:41pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583/3 "2017-09-23T15:41:59Z")

</div>

Kai,

> Roles in Ansible has the concept of files, so copy will automatically be  
> looking in files, so just use
> 
> src: bar/baz.txt

sorry, I just _new_ I’d screw up formulating my post… In fact, I had tried this already from within root/roles/mumbo/tasks/main.yaml:

- name: copy bar/baz.txt to mumbo host  
copy:
- src: bar/baz.txt
- dest: /some/other/path/baz.txt

Again, ansible compained about not finding the desired file.

Am I missing something, or is ansible incapable of referring to files living in a subdirectory in a role’s “files” folder?

Cheers –

Torsten

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 23, 2017, 3:47pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583/4 "2017-09-23T15:47:14Z")

</div>

This is a feature that is being used a lot, but in you case the syntax is not correct so I would think you get all sort of error messages.

You need to remove the dash in front of src: and dest:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 23, 2017, 6:41pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583/5 "2017-09-23T18:41:27Z")

</div>

Please keep the conversation on the list by reply to the list and not to me personally.

> > This is a feature that is being used a lot, but in you case the syntax is not correct so I would think you get all sort of error messages.
> > 
> > You need to remove the dash in front of src: and dest:
> 
> Sigh, one more typo I introduced when making up this example... Of course, I originally didn't have hyphens in front of the yaml dictionary keys.
> 
> No, ansible did explicitly complain about not finding a file inside a "files" subdirectory. Here's the original task that causes trouble:
> 
> - name: copy gerbera build script  
> become: true  
> copy:  
> src: usr/local/src/gerbera/build-gerbera.sh  
> dest: usr/local/src/gerbera/build-gerbera.sh  
> mode: ug+x
> 
> This task lives in my root/roles/gerbera\_server/tasks/main.yaml (with "root" denoting the root of the whole ansible project), while the source file lives in root/roles/gerbera\_server/files/files/usr/local/src/gerbera/

Ansible is searching roles/gerbera\_server/files  
you have files two times, then you need to use  
src: files/usr/local/src/gerbera/build-gerbera.sh

Your dest: is relative, that will probably fail, since it will be relative to where you run ansible-playbook.

> When calling the gerbera\_server role from my root/main.yaml file, I get:
> 
> TASK [gerbera\_server : copy gerbera build script] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [priamos]: FAILED! =\> {"changed": false, "failed": true, "msg": "Unable to find 'usr/local/src/gerbera/build-gerbera.sh' in expected paths."}
> 
> You're absolutely positive that paths relative to a role's "files" folder should work?

Yes, using the feature all the time.

---

<div class="post-metadata">

**Author:** ![Torsten\_Crass](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@Torsten\_Crass](https://forum.ansible.com/u/Torsten_Crass)\
**Post date:** [September 23, 2017, 8:32pm UTC](https://forum.ansible.com/t/organizing-files-in-subdirectories-at-role-level/25583/6 "2017-09-23T20:32:07Z")

</div>

> Please keep the conversation on the list by reply to the list and not to me personally.

Sorry, intended to, don't know why it didn't work... Hope it works this time!

> > No, ansible did explicitly complain about not finding a file inside a "files" subdirectory. Here's the original task that causes trouble:
> > 
> > &nbsp;&nbsp;&nbsp;- name: copy gerbera build script  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: true  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: usr/local/src/gerbera/build-gerbera.sh  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: usr/local/src/gerbera/build-gerbera.sh  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: ug+x
> > 
> > This task lives in my root/roles/gerbera\_server/tasks/main.yaml (with "root" denoting the root of the whole ansible project), while the source file lives in root/roles/gerbera\_server/files/files/usr/local/src/gerbera/

\>

> Ansible is searching roles/gerbera\_server/files  
> you have files two times, then you need to use  
> src: files/usr/local/src/gerbera/build-gerbera.sh

Oh my goodness! How could that happen -- didn't realize I accidently had an additional "files" folder within "files"! Thanks a thousand times for pointing that out -- after fixing that, it works as expected.

Cheers --

&nbsp;&nbsp;Torsten
