# defining package lists across roles

**URL:** <https://forum.ansible.com/t/defining-package-lists-across-roles/13294>\
**Category:** Ansible Project\
**Created:** [August 13, 2013, 7:16pm UTC](https://forum.ansible.com/t/defining-package-lists-across-roles/13294 "2013-08-13T19:16:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Abhijit\_Menon-Sen2](https://forum.ansible.com/u/Abhijit_Menon-Sen2)\
**Post date:** [August 13, 2013, 7:16pm UTC](https://forum.ansible.com/t/defining-package-lists-across-roles/13294/1 "2013-08-13T19:16:41Z")

</div>

Hi.

Another semi-related question arose while I was installing packages.

I have roles named "smtp-server", "imap-server", and so on; and in each  
role I have a task that does essentially the same thing:

&nbsp;&nbsp;&nbsp;&nbsp;- apt: pkg=$item state=installed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: [foo, bar, …]

Now, this works fine, but I started wondering if I could do this in one  
task[\*]. For example, I could have roles/common/tasks/packages.yml that  
does:

&nbsp;&nbsp;&nbsp;&nbsp;- apt: pkg=$item state=installed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: packages

…where "packages" is a variable defined via vars/main.yml by each role:

&nbsp;&nbsp;&nbsp;&nbsp;- packages: [postfix, dovecot, …]

This sounds good, but unfortunately, there's no way to append to the  
"packages" list in successive vars/main.yml files.

As far as I can tell, the "hash\_behaviour=merge" setting doesn't really  
help here, because it merges only hashes, not lists.

At the moment, the solution I've settled for is to remove \*all\* package  
installation from the individual roles, and just put the whole package  
list into roles/common/tasks/packages.yml. Obviously, this works, but  
it "would be nice" to have each role declare its dependencies.

Can anyone suggest a neat way to do this or something like it? Each role  
should be able to declare packages it wants, but they should be combined  
into a single apt transaction somewhere.

-- ams

1. One good reason to do this in one place is to not sprinkle handling  
&nbsp;&nbsp;&nbsp;of policy-rc.d across tasks, but I can think of other reasons. And  
&nbsp;&nbsp;&nbsp;at least one other person on IRC independently expressed a desire  
&nbsp;&nbsp;&nbsp;to build up package lists in this way in the past few days.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 14, 2013, 12:34am UTC](https://forum.ansible.com/t/defining-package-lists-across-roles/13294/2 "2013-08-14T00:34:22Z")

</div>

“This sounds good, but unfortunately, there’s no way to append to the  
“packages” list in successive vars/main.yml files.”

This is true.

You can add to hash variables if you configure the merging policy in ansible.cfg but you can’t append to variables.

It would be fine to just have seperate apt statements that operate on the specific lists – invoking apt a few extra times should be fine.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [August 14, 2013, 1:09pm UTC](https://forum.ansible.com/t/defining-package-lists-across-roles/13294/3 "2013-08-14T13:09:08Z")

</div>

Excerpts from Michael DeHaan's message of 2013-08-13 20:34:22 -0400:

> It would be fine to just have seperate apt statements that operate on the  
> specific lists -- invoking apt a few extra times should be fine.

If you're really concerned with multiple transactions, you could do  
something like:

&nbsp;&nbsp;&nbsp;&nbsp;- apt: pkg={{item}} state=installed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_flattened:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- list\_of\_common\_packages  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- list\_of\_role\_specific\_packages
