# Interpolating concatenated variables

**URL:** <https://forum.ansible.com/t/interpolating-concatenated-variables/16701>\
**Category:** Ansible Project\
**Created:** [November 5, 2014, 4:06pm UTC](https://forum.ansible.com/t/interpolating-concatenated-variables/16701 "2014-11-05T16:06:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [November 5, 2014, 4:06pm UTC](https://forum.ansible.com/t/interpolating-concatenated-variables/16701/1 "2014-11-05T16:06:41Z")

</div>

Hi,

I’d like to be able to build a variable name programatically in a similar fashion to

[http://docs.ansible.com/faq.html#how-do-i-access-a-variable-name-programmatically](http://docs.ansible.com/faq.html#how-do-i-access-a-variable-name-programmatically)

Instead of accessing a dict I would like to build up the variable name. For example

- hosts: localhost  
connection: local  
vars:  
test\_app: admin  
admin\_port: 1234  
tasks:
- debug:  
msg: “Admin port = {{ test\_app + ‘\_port’ }}”

This outputs

TASK: [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Admin port = admin\_port”  
}

I would like to get the value of **admin\_port**. So would need to do some kind of double substitution.

I need to get the information from a var file just given the app\_name. The above is just to demonstrate.

Many thanks

James

---

<div class="post-metadata">

**Author:** ![rektide](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@rektide](https://forum.ansible.com/u/rektide)\
**Post date:** [November 5, 2014, 4:31pm UTC](https://forum.ansible.com/t/interpolating-concatenated-variables/16701/2 "2014-11-05T16:31:48Z")

</div>

{{ hostvars[inventory\_hostname][test\_app + '\_port'] }} i think

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [November 5, 2014, 4:56pm UTC](https://forum.ansible.com/t/interpolating-concatenated-variables/16701/3 "2014-11-05T16:56:59Z")

</div>

Ok that doesn’t work in my example as the variables aren’t in the inventory

TASK: [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost] =\> One or more undefined variables: ‘dict’ object has no attribute ‘admin\_port’

Though If use include\_vars which I would be doing its does work

- hosts: localhost  
connection: local  
vars:  
test\_app: admin  
tasks:
- include\_vars: concat\_vars.yml
- debug:  
msg: “admin port from inventory = {{ hostvars[inventory\_hostname][test\_app+‘\_port’] }}”

TASK: [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “admin port from inventory = **1234** ”  
}

Many thanks for your help. This forum is fantastic.

James

---

<div class="post-metadata">

**Author:** ![rektide](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@rektide](https://forum.ansible.com/u/rektide)\
**Post date:** [November 5, 2014, 9:04pm UTC](https://forum.ansible.com/t/interpolating-concatenated-variables/16701/4 "2014-11-05T21:04:53Z")

</div>

The other cache for facts: vars[test\_app + '\_port']

(PITA, this)

---

<div class="post-metadata">

**Author:** ![James\_Morgan1](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@James\_Morgan1](https://forum.ansible.com/u/James_Morgan1)\
**Post date:** [November 5, 2014, 10:01pm UTC](https://forum.ansible.com/t/interpolating-concatenated-variables/16701/5 "2014-11-05T22:01:04Z")

</div>

Ah thanks for the info.
