Problems using with_fileglob

I need to install configuration files for an application on multiple hosts, where

(1) each host needs a different collection of files, and
(2) each file may have different content for each host.

I have the following file layout:

./roles/myapp/files/host01/foo.conf
./roles/myapp/files/host02/foo.conf
./roles/myapp/files/host02/bar.conf

etc etc.

In ./roles/myapp/tasks/main.yml, I want something like the following task:

  • name: install config files
    copy: dest=/etc/myapp/{{ item }} src={{ ansible_hostname }}/{{ item }}
    with_fileglob:
  • “{{ ansible_hostname }}/*.conf”

The problem is that the “with_fileglob” line sets {{ item }} to an absolute filename like

/path/to/my/dir/roles/files/host01/foo.conf

which must be copied to /etc/myapp/foo.conf on the host01 system.

Is there some way I can extract just the ‘foo.conf’ part of the full name, or does ansible have
a different way to perform such a task?

{{ path | basename }}

http://ansibleworks.com/docs/playbooks_variables.html#jinja2-filters

Ah, thank you! Somehow I managed to miss all mention of filters while scanning the docs earlier.

Just for completeness, I realised that in the particular case of the ‘copy’ module, I can just write

copy: dest=/etc/myapp src={{ item }}

and then I don’t actually need the basename in the first place.
and the problem goes away