# Variable concatenation

**URL:** <https://forum.ansible.com/t/variable-concatenation/26114>\
**Category:** Ansible Project\
**Created:** [November 23, 2017, 6:44am UTC](https://forum.ansible.com/t/variable-concatenation/26114 "2017-11-23T06:44:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [November 23, 2017, 6:44am UTC](https://forum.ansible.com/t/variable-concatenation/26114/1 "2017-11-23T06:44:29Z")

</div>

Is there a way to concatenate variables?

base:

- a
- b
- c

extend:

- x
- y
- z

Something like this?

together:

- “{{ base }”"
- extend

The above gives me

“msg”: “together [[u’a’, u’b’, u’c’], u’x’, u’y’, u’z’]”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 23, 2017, 2:33pm UTC](https://forum.ansible.com/t/variable-concatenation/26114/2 "2017-11-23T14:33:31Z")

</div>

> Is there a way to concatenate variables?

Since you variable contains a list you can use the union filter  
[https://docs.ansible.com/ansible/latest/playbooks\_filters.html](https://docs.ansible.com/ansible/latest/playbooks_filters.html)

But this will remove duplicates or just use +.

> base:  
> &nbsp;&nbsp;- a  
> &nbsp;&nbsp;- b  
> &nbsp;&nbsp;- c
> 
> extend:  
> &nbsp;&nbsp;- x  
> &nbsp;&nbsp;- y  
> &nbsp;&nbsp;- z
> 
> Something like this?
> 
> together:  
> &nbsp;&nbsp;- "{{ base }""  
> &nbsp;&nbsp;- extend
> 
> The above gives me
> 
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": "together [[u'a', u'b', u'c'], u'x', u'y', u'z']"

{{ base | union(extend) }}

{{ base + extend }}
