# concatenate a value with another variable in default filter

**URL:** <https://forum.ansible.com/t/concatenate-a-value-with-another-variable-in-default-filter/23388>\
**Category:** Ansible Project\
**Created:** [January 5, 2017, 2:10pm UTC](https://forum.ansible.com/t/concatenate-a-value-with-another-variable-in-default-filter/23388 "2017-01-05T14:10:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [January 5, 2017, 2:10pm UTC](https://forum.ansible.com/t/concatenate-a-value-with-another-variable-in-default-filter/23388/1 "2017-01-05T14:10:58Z")

</div>

I have the bellow Ansible inventory :

`  
[web:children]  
web-eu-west  
web-us-east  
[eu:vars]  
dc=eu-west  
[eu:children]  
web-eu-west  
[us:vars]  
dc=us-east  
[us:children]  
web-us-east

`

The bellow is working fine in my playbook :  
`  
– hosts: “{{ hosts | default(‘web’) }}”

`

Unfortunately we have a case now where we need to combine the **dc** variable in the default filter , I’m trying the bellow two combinations with errors :

`

- hosts: “{{ hosts | default('web-'dc) }}”  
`

> ERROR! template error while templating string: expected token ‘,’, got ‘dc’. String: {{ hosts | default('web-'dc) }}

`

- hosts: “{{ hosts | default(‘bid-’+ dc) }}”  
`

> ERROR! the field ‘hosts’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘dc’ is undefined

What is the right way to concatenate a value and a var using the default filter ?

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 5, 2017, 3:06pm UTC](https://forum.ansible.com/t/concatenate-a-value-with-another-variable-in-default-filter/23388/2 "2017-01-05T15:06:50Z")

</div>

Try this:

- hosts: "{{ hosts | default([[ 'web-', dc] | join ] ) }}"  
or  
- hosts: "{{ hosts | default(['web-', dc] | join ) }}"

Not sure if you need two sets of square brackets.

Johannes

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 5, 2017, 3:45pm UTC](https://forum.ansible.com/t/concatenate-a-value-with-another-variable-in-default-filter/23388/3 "2017-01-05T15:45:07Z")

</div>

Your general approach is pretty much correct in your last example:

- hosts: “{{ hosts | default(‘bid-’+ dc) }}”

Although jinja2 recommends using a `~` instead of `+` for string concatenation, although both will work.

However, the real problem is with your variable. You have created the `dc` variable in your inventory, which means that it is scoped to the hosts within that group. Because you are using the `dc` variable in your `hosts` declaration, no hosts have been targeted, and additionally, the `dc` variable is per host, not global.

As such, you cannot use the `dc` variable in a hosts declaration due to variable scoping, and the `dc` variable not being defined at that point.

The only real place to define a variable that can be used in the `hosts` declaration is via the command line via -e/–extra-vars

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [January 5, 2017, 5:05pm UTC](https://forum.ansible.com/t/concatenate-a-value-with-another-variable-in-default-filter/23388/4 "2017-01-05T17:05:39Z")

</div>

Got it Matt , that makes sense.

Thanks .
