Organizing files in subdirectories at role-level

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

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

src: bar/baz.txt

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

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:

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.

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:

   - 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

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 --

  Torsten