# Combining lists

**URL:** <https://forum.ansible.com/t/combining-lists/14607>\
**Category:** Ansible Project\
**Created:** [February 20, 2014, 6:25pm UTC](https://forum.ansible.com/t/combining-lists/14607 "2014-02-20T18:25:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Gargan](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Stephen\_Gargan](https://forum.ansible.com/u/Stephen_Gargan)\
**Post date:** [February 20, 2014, 6:25pm UTC](https://forum.ansible.com/t/combining-lists/14607/1 "2014-02-20T18:25:10Z")

</div>

I’d like to be able to do the following, hopefully someone can point me to a simple way to do it.

I have two lists, one contains vars for all hosts another vars for a specific host. I’d like to join them into a single list that can be iterated over in a template. I reuse the template so would rather it did not know about two separate lists.

generic\_vars: [‘a’, ‘b’, ‘c’]  
specific\_vars: [‘d’, ‘e’]

somehow I’d like to combine these into an all\_vars variable, so that in the template I can do the following.

mytemplate.json

{  
“vars”: [  
{% for v in all\_vars %}  
“{{v}}”{% if not loop.last %},{% endif %}  
{% endfor %}  
]  
}

This is a patten that arises a lot for me so I’m probably going to write a module to do it. But I thought I’d check to see if there was a simple standard way of doing so before heading down that path.

Is this something that can be easily done?

thanks,

Steve.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 20, 2014, 6:27pm UTC](https://forum.ansible.com/t/combining-lists/14607/2 "2014-02-20T18:27:38Z")

</div>

Here’s some documentation on set theory filters that may help:

[http://docs.ansible.com/playbooks\_variables.html#id22](http://docs.ansible.com/playbooks_variables.html#id22)

---

<div class="post-metadata">

**Author:** ![Stephen\_Gargan](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Stephen\_Gargan](https://forum.ansible.com/u/Stephen_Gargan)\
**Post date:** [February 20, 2014, 11:39pm UTC](https://forum.ansible.com/t/combining-lists/14607/3 "2014-02-20T23:39:47Z")

</div>

Thanks for the pointer Michael, here’s what I ended up doing.

{  
“combined”: [“{{first\_list | union(second\_list) | join('”, “')}}”]

}
