# Accessing hashes of lists

**URL:** <https://forum.ansible.com/t/accessing-hashes-of-lists/16689>\
**Category:** Ansible Project\
**Created:** [November 4, 2014, 4:55pm UTC](https://forum.ansible.com/t/accessing-hashes-of-lists/16689 "2014-11-04T16:55:20Z")\
**Posts on this page:** 3\
**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 4, 2014, 4:55pm UTC](https://forum.ansible.com/t/accessing-hashes-of-lists/16689/1 "2014-11-04T16:55:20Z")

</div>

Hi,

I need to store information about applications in a variable data structure and access it via key.

If I store the information as follows I can access using **with\_items**

- hosts: localhost  
connection: local  
vars:  
apps:  
admin:
- service: admin  
handler: /admin  
port: 1200  
…  
rpt:
- service: rpt  
handler: /rpt  
port: 1234  
…  
tasks:
- debug:  
msg: “{{ item.service }} = {{ item.port }}”  
with\_items: apps.admin

TASK: [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item={‘handler’: ‘/admin’, ‘port’: 1200, ‘service’: ‘admin’}) =\> {  
“item”: {  
“handler”: “/admin”,  
“port”: 1200,  
“service”: “admin”  
},  
“msg”: “admin = 1200”  
}

How can I access one of the nested variables directly keying on application. The following won’t work.

- debug:  
msg: “{{ apps.admin.service }}”

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

I can’t use **with\_items** as I need to call lots of roles. I don’t want to pass the information directly to the roles as variables as I want to have it defined in one place and it needs to be used in multiple places.

I.e application configuration and apache virtual host definitions.

Any help most appreciated.

I’m happy to change the structure. All I need to do is store an amount of information keyed by application

Thanks

James

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [November 4, 2014, 4:59pm UTC](https://forum.ansible.com/t/accessing-hashes-of-lists/16689/2 "2014-11-04T16:59:01Z")

</div>

In your example, `apps.admin` actually contains a list of dictionaries. So accessing it would be: `apps.admin[0].service`.

---

<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 4, 2014, 5:11pm UTC](https://forum.ansible.com/t/accessing-hashes-of-lists/16689/3 "2014-11-04T17:11:47Z")

</div>

Thanks, I think I need to reconsider my data structure.

I need to be able to iterate over applications in a group in some situations but also key in by application name in others

I’ll have a think.

Thanks
