# accessing nested variables in dictionary that contains ansible-variable as key

**URL:** <https://forum.ansible.com/t/accessing-nested-variables-in-dictionary-that-contains-ansible-variable-as-key/34515>\
**Category:** Ansible Project\
**Created:** [January 26, 2021, 11:34pm UTC](https://forum.ansible.com/t/accessing-nested-variables-in-dictionary-that-contains-ansible-variable-as-key/34515 "2021-01-26T23:34:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_Svensson](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@Philip\_Svensson](https://forum.ansible.com/u/Philip_Svensson)\
**Post date:** [January 26, 2021, 11:34pm UTC](https://forum.ansible.com/t/accessing-nested-variables-in-dictionary-that-contains-ansible-variable-as-key/34515/1 "2021-01-26T23:34:09Z")

</div>

Hey I’m wondering how I should point to variables nested like shown below.  
if it helps, the playbook I’m writing doesn’t need to work with multiple hosts at once

The variable cannot be re-structured since it’s being used by another ansible-module I do not maintain

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 27, 2021, 3:43am UTC](https://forum.ansible.com/t/accessing-nested-variables-in-dictionary-that-contains-ansible-variable-as-key/34515/2 "2021-01-27T03:43:45Z")

</div>

> how I should point to variables nested like shown below.
> 
> &nbsp;&nbsp;vm\_specs:  
> &nbsp;&nbsp;&nbsp;&nbsp;"{{ hostname }}":  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns1: 1.1.1.1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns2: 2.2.2.2

For example,

&nbsp;&nbsp;\> cat playbook.yml  
&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_specs:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"{{ hostname }}":  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns1: 1.1.1.1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns2: 2.2.2.2  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hostname: "{{ vm\_specs.keys()|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_dns: "{{ vm\_specs.values()|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_hostname  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_dns

gives

&nbsp;&nbsp;\> ansible-playbook playbook.yml -e hostname=test

&nbsp;&nbsp;TASK [debug]
