# How-to Variable in Variable

**URL:** <https://forum.ansible.com/t/how-to-variable-in-variable/22912>\
**Category:** Ansible Project\
**Created:** [October 28, 2016, 9:26am UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912 "2016-10-28T09:26:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonheur.sorn](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@bonheur.sorn](https://forum.ansible.com/u/bonheur.sorn)\
**Post date:** [October 28, 2016, 9:26am UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912/1 "2016-10-28T09:26:43Z")

</div>

Hi everyone

I’m new to Ansible and I’m trying to automate the generation of Cisco router configuration files.

For the same config template, I will have several hardware models and I’d like to keep the code simple.

`  
#main.yml (roles tasks)

---

<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:** [October 28, 2016, 2:45pm UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912/2 "2016-10-28T14:45:56Z")

</div>

"How do I access a variable name programmatically" is the correct way to do it  
[https://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-name-programmatically](https://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-name-programmatically)

interface {{ hostvars[inventory\_hostname][item.model]['wan'] }}

---

<div class="post-metadata">

**Author:** ![bonheur.sorn](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@bonheur.sorn](https://forum.ansible.com/u/bonheur.sorn)\
**Post date:** [October 29, 2016, 10:42am UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912/3 "2016-10-29T10:42:27Z")

</div>

Hi

Still have an error

_failed: [NRT001] (item={u’model’: u’NRT4451’, u’hostname’: u’NRT001’}) =\> {“failed”: true, “item”: {“hostname”: “NRT001”, “model”: “NRT4451”}, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute u’NRT4451’”}_

---

<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:** [October 29, 2016, 11:06am UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912/4 "2016-10-29T11:06:21Z")

</div>

Is your variable definition in the roles vars/main.yml, if so this should work.

interface {{ vars[item.model]['wan'] }}

---

<div class="post-metadata">

**Author:** ![Vikas\_Kumar](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@Vikas\_Kumar](https://forum.ansible.com/u/Vikas_Kumar)\
**Post date:** [October 29, 2016, 11:32am UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912/5 "2016-10-29T11:32:47Z")

</div>

Have a look [here](http://stackoverflow.com/questions/32976700/python-jinja2-variable-inside-a-variable/32984309).

---

<div class="post-metadata">

**Author:** ![bonheur.sorn](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@bonheur.sorn](https://forum.ansible.com/u/bonheur.sorn)\
**Post date:** [October 29, 2016, 12:23pm UTC](https://forum.ansible.com/t/how-to-variable-in-variable/22912/6 "2016-10-29T12:23:27Z")

</div>

interface {{ vars[item.model][‘wan’] }} works like a charm.  
Thanks guys! really appreciate!

I was struggling to find the magic predefined variable name to point to vars/main.yml.  
Where is that documented? I looked [here](http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)and [here](http://stackoverflow.com/questions/18839509/where-can-i-get-a-list-of-ansible-pre-defined-variables)
