# HowTo: combine two lists in vars section/file

**URL:** <https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017>\
**Category:** Ansible Project\
**Created:** [March 30, 2016, 9:45am UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017 "2016-03-30T09:45:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jard\_Leex](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jard\_Leex](https://forum.ansible.com/u/Jard_Leex)\
**Post date:** [March 30, 2016, 9:45am UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017/1 "2016-03-30T09:45:06Z")

</div>

Hello together,

i’d like to combine two lists directly in a vars section/file.  
I’ve tried the combine filter but as i understood it, it is for dicts and not lists.  
Also the union filter did not worked for me as i tried it  
I assume that i did something wrong.

Can someone here make the example below fly?

`

---

<div class="post-metadata">

**Author:** ![Jard\_Leex](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jard\_Leex](https://forum.ansible.com/u/Jard_Leex)\
**Post date:** [March 30, 2016, 2:00pm UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017/2 "2016-03-30T14:00:05Z")

</div>

To make my request a bit more clear:

I’d like to have the entries of _`level1keyB`_` in the path list of the hostname bar. The key 'addonpath' is just an guessed construct by me. Maybe this would also work.`

` `

`- level1keyA:`  
`- hostname: foo`  
`state: present`  
`path:`  
`- left`  
`- hostname: bar`  
`state: present`  
`path:`  
`- righthere`  
`- "{{ entries of level1keyB }}"`

` `  
`But how to do it in practise?`

`Thank you.`

---

<div class="post-metadata">

**Author:** ![John\_Buxton](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@John\_Buxton](https://forum.ansible.com/u/John_Buxton)\
**Post date:** [March 31, 2016, 11:07pm UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017/3 "2016-03-31T23:07:31Z")

</div>

In your second example, you could do this:

`  
path: “{{ [‘righthere’] | union(level1keyB) }}”

`

I’m not sure it’s possible in your first example, because to put this in your “WHAT TO WRITE HERE” section you have to access “addonpath” from another part of the same level1keyA data structure, so you end up with a recursive loop error.

---

<div class="post-metadata">

**Author:** ![Jard\_Leex](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jard\_Leex](https://forum.ansible.com/u/Jard_Leex)\
**Post date:** [April 1, 2016, 7:12am UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017/4 "2016-04-01T07:12:30Z")

</div>

Hi John Buxton,

thanks for your reply.  
Indeed that worked.

`

---

<div class="post-metadata">

**Author:** ![Jard\_Leex](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jard\_Leex](https://forum.ansible.com/u/Jard_Leex)\
**Post date:** [July 8, 2016, 8:48am UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017/5 "2016-07-08T08:48:21Z")

</div>

Hi,

anyone else got an idea how to solve this?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Jard\_Leex](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jard\_Leex](https://forum.ansible.com/u/Jard_Leex)\
**Post date:** [August 31, 2016, 6:59am UTC](https://forum.ansible.com/t/howto-combine-two-lists-in-vars-section-file/21017/6 "2016-08-31T06:59:41Z")

</div>

Hi,

to calrify the question here comes a shorten code block

`
