# How to concat variables and assign to a new variable in template?

**URL:** <https://forum.ansible.com/t/how-to-concat-variables-and-assign-to-a-new-variable-in-template/14504>\
**Category:** Ansible Project\
**Created:** [February 6, 2014, 4:23am UTC](https://forum.ansible.com/t/how-to-concat-variables-and-assign-to-a-new-variable-in-template/14504 "2014-02-06T04:23:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![howachen](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@howachen](https://forum.ansible.com/u/howachen)\
**Post date:** [February 6, 2014, 4:23am UTC](https://forum.ansible.com/t/how-to-concat-variables-and-assign-to-a-new-variable-in-template/14504/1 "2014-02-06T04:23:16Z")

</div>

Hi,

For example, in template,

{% if foo is defined and foo == “apple” %}

# I want to set foo equal to the concatenation of x + “,” + y + “,” + z, how?

{% endif %}

{{ foo }}

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [February 6, 2014, 4:48am UTC](https://forum.ansible.com/t/how-to-concat-variables-and-assign-to-a-new-variable-in-template/14504/2 "2014-02-06T04:48:38Z")

</div>

try:

{% set foo = x ~ "," ~ y ~ "," ~ z %}

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925
