# Collecting files across all the roles a host belongs to

**URL:** <https://forum.ansible.com/t/collecting-files-across-all-the-roles-a-host-belongs-to/17471>\
**Category:** Ansible Project\
**Created:** [February 9, 2015, 1:40pm UTC](https://forum.ansible.com/t/collecting-files-across-all-the-roles-a-host-belongs-to/17471 "2015-02-09T13:40:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JK\_Laiho](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@JK\_Laiho](https://forum.ansible.com/u/JK_Laiho)\
**Post date:** [February 9, 2015, 1:40pm UTC](https://forum.ansible.com/t/collecting-files-across-all-the-roles-a-host-belongs-to/17471/1 "2015-02-09T13:40:04Z")

</div>

I’m new to Ansible, and trying to wrap my head around the variety of ways to organize playbooks and achieve certain tasks in a DRY way, without duplicating too much logic in several places.

Right now I’m trying to find a way in which I could have with\_fileglob consolidate files of a certain pattern from all of the roles that a host belongs to. Specifically, some roles contain apt keys, some roles don’t.

The way I envisioned setting this up is as follows. First, the role hierarchy:

roles/  
common/  
files/  
… # no apt keys for this role  
db/  
files/  
… # no apt keys for this role  
rabbitmq/  
files/  
apt\_keys/  
some\_rabbitmq\_specific\_key.asc  
web/  
files/  
apt\_keys/  
some\_web\_specific\_key.asc

The task in roles/common/tasks/main.yml:

- name: add apt keys  
apt\_key: file=“{{ item }}”  
sudo: true  
with\_fileglob:
- apt\_keys/\*.asc

In my ideal world, if a host belongs to common and web, with\_fileglob would add some\_web\_specific\_key.asc. If a host belongs to common, db and rabbitmq, it would add some\_rabbitmq\_specific\_key.asc.

As you’ve probably guessed from the fact that I’m writing this post, this approach doesn’t work. The task, placed inside common, is simply skipped, apparently because the common role contains no apt keys (and even if it did, it would still ignore the keys in rabbitmq and web).

Is this type of cross-role file aggregation possible? I can sort of see why it wouldn’t be, though in my current circumstance it would seem very useful.

The deeper rationale here is that I want a certain other task to get run after **all** the apt keys that apply to a host through **all of its role memberships** have been added. Had the approach above worked, I would have simply placed this task right after the apt\_key task in common/tasks/main.yml, but since it doesn’t, I don’t know how to achieve this. Suggestions?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [February 10, 2015, 1:25am UTC](https://forum.ansible.com/t/collecting-files-across-all-the-roles-a-host-belongs-to/17471/2 "2015-02-10T01:25:19Z")

</div>

Hi,

I believe if you want to keep this in a common role, the best way would be to simply list each task to install the key as follows:

- name: install rabbitmq key  
apt\_key: file="apt\_keys/some\_rabbitmq\_specific\_key.asc  
when: “rabbitmq” in group\_names

…

If you wanted to make it more generic, you could store the group names/keys in some sort of variable structure and loop over that, but as you noted the approach you’re taking won’t work as with\_fileglob only operates on the directory relative to the current role being run.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![JK\_Laiho](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@JK\_Laiho](https://forum.ansible.com/u/JK_Laiho)\
**Post date:** [February 10, 2015, 11:55am UTC](https://forum.ansible.com/t/collecting-files-across-all-the-roles-a-host-belongs-to/17471/3 "2015-02-10T11:55:15Z")

</div>

Hi James,

Thanks for your help! You put me on the right track. Here’s what I ended up doing (in case someone else finds this useful).

First, for DRY’s sake, I collected the absolute paths to the apt keys in group\_vars/all to make them “global” and role-independent, in a form that could be consumed using with\_subelements:
