# Dictionary elements access within group\_vars

**URL:** <https://forum.ansible.com/t/dictionary-elements-access-within-group-vars/18801>\
**Category:** Ansible Project\
**Created:** [July 17, 2015, 5:29pm UTC](https://forum.ansible.com/t/dictionary-elements-access-within-group-vars/18801 "2015-07-17T17:29:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Siechniewicz](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Daniel\_Siechniewicz](https://forum.ansible.com/u/Daniel_Siechniewicz)\
**Post date:** [July 17, 2015, 5:29pm UTC](https://forum.ansible.com/t/dictionary-elements-access-within-group-vars/18801/1 "2015-07-17T17:29:36Z")

</div>

Hi,

I’m using ansible[-playbook] 1.9.0.1.

I have 4 “proxy” hosts talking to 2 “app” hosts:

proxy1 → app1  
proxy2 → app1  
proxy3 → app2  
proxy4 → app2

In group vars I defined:

proxy\_to\_app\_map:  
1: 1  
2: 1  
3: 2  
4: 2

proxy\_no: “{{ ansible\_hostname | regex\_replace(‘^.\*([0-9]+)$’, ‘\\1’) }}”  
app\_no: “{{ proxy\_to\_app\_map[proxy\_no] }}”

I’m getting:

ok: [proxy1] =\> {  
“var”: {  
“proxy\_no”: “1”  
}  
}

and so on, but then I get a string instead of a specific dictionary value (I expect a number, 1 or 2).

ok: [proxy1] =\> {

“var”: {

“app\_no”: “{{ proxy\_to\_app\_map[proxy\_no] }}”

}

}

I tried this:  
app\_no: “{{ proxy\_to\_app\_map.proxy\_no }}”

And this  
app\_no: proxy\_to\_app\_map.proxy\_no

And this  
app\_no: “proxy\_to\_app\_map.proxy\_no”

and it still does not access the value. Why does this (not) happen? Is there any documentation somewhere that would describe the rules of what gets interpolated and what doesn’t?

I have something like that later on, but it won’t work without proper app\_no.

app\_peer\_host: “{{ ansible\_hostname | regex\_replace(‘^proxy’, ‘app’) | regex\_replace(proxy\_no + ‘$’, app\_no) }}”  
app\_peer\_ip: “{{ hostvars[app\_peer\_host][‘ansible\_eth0’][‘ipv4’][‘address’] }}”

I’m using the hostvars trick like in the app\_peer\_ip in many places so some interpolation works within group vars, but not all. Is this because hostvars is a dict defined “externally” and proxy\_to\_app\_map internally?

A workaround is something like that:

proxy\_to\_app\_map:  
proxy1: app1  
proxy2: app1  
proxy3: app2  
proxy4: app2

app\_peer\_host: “{{ proxy\_to\_app\_map[ansible\_hostname] }}”

Which does work, and looks simpler, but would need to be defined for every environment separately (real hostnames are more complex than proxy and app), and that’s lots of defining.

This leads me to a conclusion that a variable defined within group\_vars cannot be used as a key “identifier” to access a dictionary element if that dictionary is also defined in group\_vars. Is that the case?

Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_Siechniewicz](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Daniel\_Siechniewicz](https://forum.ansible.com/u/Daniel_Siechniewicz)\
**Post date:** [July 20, 2015, 12:51pm UTC](https://forum.ansible.com/t/dictionary-elements-access-within-group-vars/18801/2 "2015-07-20T12:51:20Z")

</div>

Replying to myself.

Well, it was just integer vs. string in the end. So yeah, PEBKAC. Quoting everything in proxy\_to\_app\_map did the trick.

Regards,  
Daniel
