# Ansible evaulate dynamic varaible name from vars

**URL:** <https://forum.ansible.com/t/ansible-evaulate-dynamic-varaible-name-from-vars/23567>\
**Category:** Ansible Project\
**Created:** [January 26, 2017, 1:18pm UTC](https://forum.ansible.com/t/ansible-evaulate-dynamic-varaible-name-from-vars/23567 "2017-01-26T13:18:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yordan\_Borisov](https://avatars.discourse-cdn.com/v4/letter/y/d26b3c/32.png) [@Yordan\_Borisov](https://forum.ansible.com/u/Yordan_Borisov)\
**Post date:** [January 26, 2017, 1:18pm UTC](https://forum.ansible.com/t/ansible-evaulate-dynamic-varaible-name-from-vars/23567/1 "2017-01-26T13:18:15Z")

</div>

I have vars where I put something like this:

vars/main.yml

> hello\_port: 80

> world\_port: 81

in my ansbile file I load the vars with

vars\_files:

- ./vars/main.yml

and after that I have task with iterate using with\_items:

- debug:  
msg: “{{ (item + ‘\_port’) }}”  
with\_items: “{{ m\_name }}”

But I’ve got as output  
hello\_port  
world\_port

not the values.  
Is there a way to evaluate the dynamically created variables name - to get the value?

---

<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 27, 2017, 7:54am UTC](https://forum.ansible.com/t/ansible-evaulate-dynamic-varaible-name-from-vars/23567/2 "2017-01-27T07:54:51Z")

</div>

> I have vars where I put something like this:
> 
> vars/main.yml
> 
> hello\_port: 80
> 
> world\_port: 81
> 
> in my ansbile file I load the vars with  
> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ./vars/main.yml
> 
> and after that I have task with iterate using with\_items:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ (item + '\_port') }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ m\_name }}"

What does the variable m\_name contain?

> But I've got as output  
> hello\_port  
> world\_port

I think your task is missing the lookup, i.e. it puts out the names of  
the variable, that you want to get the value of.

Try this as the msg-line:  
msg: "{{ [(item + '\_port')] }}"

Or maybe you need to use hostvars syntax...

Johannes

---

<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:** [January 27, 2017, 8:39am UTC](https://forum.ansible.com/t/ansible-evaulate-dynamic-varaible-name-from-vars/23567/3 "2017-01-27T08:39:43Z")

</div>

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ vars[item + '\_port'] }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ m\_name }}"

---

<div class="post-metadata">

**Author:** ![Yordan\_Borisov](https://avatars.discourse-cdn.com/v4/letter/y/d26b3c/32.png) [@Yordan\_Borisov](https://forum.ansible.com/u/Yordan_Borisov)\
**Post date:** [February 1, 2017, 10:52am UTC](https://forum.ansible.com/t/ansible-evaulate-dynamic-varaible-name-from-vars/23567/4 "2017-02-01T10:52:40Z")

</div>

Thanks 🙂
