# Accumulating a list in Ansible

**URL:** <https://forum.ansible.com/t/accumulating-a-list-in-ansible/18802>\
**Category:** Ansible Project\
**Created:** [July 17, 2015, 11:32pm UTC](https://forum.ansible.com/t/accumulating-a-list-in-ansible/18802 "2015-07-17T23:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Raison](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@Kevin\_Raison](https://forum.ansible.com/u/Kevin_Raison)\
**Post date:** [July 17, 2015, 11:32pm UTC](https://forum.ansible.com/t/accumulating-a-list-in-ansible/18802/1 "2015-07-17T23:32:12Z")

</div>

Hello, group. I am still a novice with Ansible, so perhaps my approach is flawed, so if there is a better way, I am happy to hear about it. I have a situation where I need to accumulate values into an Ansible list variable on a role-by-role basis, and then use the contents of that list in another role in order to populate a config file template. So, suppose I have these roles:

- roles:
- service1
- service2
- service3
- make-config-file

And a variable ‘services’. I would like to end up with services having a value of [service1, service2, service3] by the time ‘make-config-file’ is executed so that it can write those values out to the config file.

So, the question is, how do I push a value onto the ‘services’ list in each role? I understand how to get the values from the list into the config file, I am just not sure how to accumulate a list in Ansible.

Thanks,  
Kevin

---

<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:** [July 19, 2015, 2:52am UTC](https://forum.ansible.com/t/accumulating-a-list-in-ansible/18802/2 "2015-07-19T02:52:40Z")

</div>

you can use union filter:

services: "{{service1|union(service2)|union(service3)}}"

---

<div class="post-metadata">

**Author:** ![Abhijit\_Menon-Sen1](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Abhijit\_Menon-Sen1](https://forum.ansible.com/u/Abhijit_Menon-Sen1)\
**Post date:** [July 20, 2015, 1:05am UTC](https://forum.ansible.com/t/accumulating-a-list-in-ansible/18802/3 "2015-07-20T01:05:59Z")

</div>

Brian, thanks for the tip.

Is there any way to do the moral equivalent of "list\_behaviour=append"?  
In other words, to be able to append items to an already-declared list?

I originally wanted this so that roles could declare all the packages  
they needed, and they could be installed in one go by a common task.  
There are many other (better) reasons, e.g. modifying my PostgreSQL  
configuration if I'm also installing the postgres\_replication role.

Would anyone else find this useful?

-- Abhijit

P.S. I'm open to suggestions for some way to do this using a local  
plugin of some sort. I just want to get it to work somehow.
