# collecting "items" across roles and then iterating over them in a handler

**URL:** <https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195>\
**Category:** Ansible Project\
**Created:** [January 6, 2015, 1:55pm UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195 "2015-01-06T13:55:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Macdonald-Wa](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Matthew\_Macdonald-Wa](https://forum.ansible.com/u/Matthew_Macdonald-Wa)\
**Post date:** [January 6, 2015, 1:55pm UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195/1 "2015-01-06T13:55:49Z")

</div>

Hi all,

We have a number of clients for whom we manage systems using Ansible. I’m fairly new to Ansible however my background is heavy use of Chef and Puppet so I understand most of the concepts.

In order to make our lives easier, we have a central “ansible” git repo containing generic roles. This is then cloned as a submodule into each client repo where we override variables and config files accordingly to make the “generic” roles from the master repo client specific.

One of the roles that we have installs and configures Jenkins CI Server. We have a set of “base” jenkins plugins that we install as part of the Jenkins role, however for some clients we will want to install and configure additional plugins.

Is there any way within Ansible that I can collect a list of these plugins from various roles (git, svn, capistrano etc.) and then have them installed by the Jenkins role?

The current approach is to have a “command” task in each role to execute the jenkins-cli command to install the plugins which seems like a very un-DRY way of doing things.

I’m reasonably happy with Python so I’m happy to write a module if this is what is required, however I’m hoping that this is a solved problem!

Thanks in advance for any help,

Matt

P.S. FWIW, it looks like [https://github.com/ansible/ansible/pull/6674](https://github.com/ansible/ansible/pull/6674) might solve this for me, but it’s not been merged as far as I can tell.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 7, 2015, 1:12pm UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195/2 "2015-01-07T13:12:56Z")

</div>

IIRC you can use the jenkins web api for listing/installing plugins  
(using uri module)

---

<div class="post-metadata">

**Author:** ![Matthew\_Macdonald-Wa](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Matthew\_Macdonald-Wa](https://forum.ansible.com/u/Matthew_Macdonald-Wa)\
**Post date:** [January 9, 2015, 10:08am UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195/3 "2015-01-09T10:08:15Z")

</div>

Hi Bryan and thanks for the reply.

The problem I’m trying to solve is collecting multiple jenkins plugins across roles but only executing the installation of them once.

At the moment, I have to list and then install the plugins in each individual /tasks/main.yml whereas what I’d like to do is something like this:

group\_vars/server\_type\_1

jenkins\_modules:

- git
- ansiicolor

group\_vars/server\_type\_2  
jenkins\_modules:

- svn
- pipeline

group\_vars/all  
jenkins\_modules:

- chuck-norris

and then have a handler or similar install the relevant plugins (for example a host in server\_type\_1 group would have chuck-norris, git and ansiicolor installed, whereas a host in server\_type\_2 would have chuck-norris, svn and pipeline installed instead).

I hope this makes things a bit clearer.

Matt

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 9, 2015, 3:05pm UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195/4 "2015-01-09T15:05:37Z")

</div>

name the vars differently so they don't override, then you can  
mix/match as you please

type1\_jenkins\_mods:  
- git

type2\_jenkins\_mods:  
&nbsp;&nbsp;&nbsp;&nbsp;- svn  
&nbsp;&nbsp;&nbsp;&nbsp;- pipeline

common\_jenkins\_mods:  
&nbsp;&nbsp;- chuck-norris

they you can

with\_items: "{{common\_jenkins\_mods|union(type1\_jenkins\_mods)}}"

for example.

---

<div class="post-metadata">

**Author:** ![Mark\_Janssen](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@Mark\_Janssen](https://forum.ansible.com/u/Mark_Janssen)\
**Post date:** [January 9, 2015, 3:51pm UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195/5 "2015-01-09T15:51:05Z")

</div>

Note that this only works in 1.7.x and beyond … I just ran it on a host still running 1.6.10 and couldn’t get it to work until upgrading to 1.7.x

Mark

---

<div class="post-metadata">

**Author:** ![Matthew\_Macdonald-Wa](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Matthew\_Macdonald-Wa](https://forum.ansible.com/u/Matthew_Macdonald-Wa)\
**Post date:** [January 12, 2015, 12:11pm UTC](https://forum.ansible.com/t/collecting-items-across-roles-and-then-iterating-over-them-in-a-handler/17195/6 "2015-01-12T12:11:41Z")

</div>

Thanks both,

I’ll have a play with that and see what I can get 🙂

Matt
