# Retrieving the dictionary key from the dictionary variable

**URL:** <https://forum.ansible.com/t/retrieving-the-dictionary-key-from-the-dictionary-variable/20295>\
**Category:** Ansible Project\
**Created:** [January 20, 2016, 9:52pm UTC](https://forum.ansible.com/t/retrieving-the-dictionary-key-from-the-dictionary-variable/20295 "2016-01-20T21:52:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igor\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@Igor\_Kaplan](https://forum.ansible.com/u/Igor_Kaplan)\
**Post date:** [January 20, 2016, 9:52pm UTC](https://forum.ansible.com/t/retrieving-the-dictionary-key-from-the-dictionary-variable/20295/1 "2016-01-20T21:52:01Z")

</div>

Hi all,

Could someone please help me with the following issue.  
I have created the EC2 security group using the  
ec2\_group module.

ec2\_group  
name: group name  
description: group description  
rules:

- proto: tcp  
to\_port: 22  
cidr\_ip: 127.0.0.1  
…  
register: sg

If I display the sg variable using the debug, I see all security group information.

Now I need to retrieve the rules array from the sg variable, actually need to retrieve the cidr\_ip from rules and I am not sure how.

I tried the following: sg.rules[0].cidr\_ip, gave me an incorrect variable error. It even gives me the incorrect “rules” variable if I do something like  
sg.rules

However I can do sg.group\_id with no problems.

So I am probably missing something simple. Could someone please tell me what is the proper way to retrieve the dictionary value, for the key, which is the dictionary or array of dictionaries itself.

Thanks so much in advance for any help.

-Igor.
