# Selecting subelements from variable

**URL:** <https://forum.ansible.com/t/selecting-subelements-from-variable/18495>\
**Category:** Ansible Project\
**Created:** [June 14, 2015, 9:12am UTC](https://forum.ansible.com/t/selecting-subelements-from-variable/18495 "2015-06-14T09:12:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viktor\_Szathmary](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Viktor\_Szathmary](https://forum.ansible.com/u/Viktor_Szathmary)\
**Post date:** [June 14, 2015, 9:12am UTC](https://forum.ansible.com/t/selecting-subelements-from-variable/18495/1 "2015-06-14T09:12:37Z")

</div>

Hi,

Let’s say I have a variable structure like this:

vpc\_subnets:

- cidr: **“172.22.1.0/24”**  
az: “us-east-1a”

- cidr: **“172.22.2.0/24”**  
az: “us-east-1b”

- cidr: **“172.22.3.0/24”**  
az: “us-east-1c”

… and a task using it:

ec2\_vpc:  
…  
subnets: “{{ vpc\_subnets }}”  
route\_tables:

- subnets:  
**- “172.22.1.0/24”**  
**- “172.22.2.0/24”**  
**- “172.22.3.0/24”**  
routes:

- dest: 0.0.0.0/0  
gw: igw

I would like to avoid repeating the CIDRs in route\_tables/subnets, and just select all the CIDRs from the vpc\_subnets variable. Is there a nice way of doing this?

Thanks,  
Viktor

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [June 15, 2015, 12:52pm UTC](https://forum.ansible.com/t/selecting-subelements-from-variable/18495/2 "2015-06-15T12:52:51Z")

</div>

can you please try "

vpc\_subnets | map(attribute=‘cidr’)| list

---

<div class="post-metadata">

**Author:** ![Viktor\_Szathmary](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@Viktor\_Szathmary](https://forum.ansible.com/u/Viktor_Szathmary)\
**Post date:** [June 16, 2015, 10:13am UTC](https://forum.ansible.com/t/selecting-subelements-from-variable/18495/3 "2015-06-16T10:13:15Z")

</div>

Thanks Benno, that worked!

– Viktor
