# Extracting inner (nested) elements from a list, or launching EC2 instances on more than one zone

**URL:** <https://forum.ansible.com/t/extracting-inner-nested-elements-from-a-list-or-launching-ec2-instances-on-more-than-one-zone/19577>\
**Category:** Ansible Project\
**Created:** [October 21, 2015, 4:11pm UTC](https://forum.ansible.com/t/extracting-inner-nested-elements-from-a-list-or-launching-ec2-instances-on-more-than-one-zone/19577 "2015-10-21T16:11:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan1](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tzach\_Livyatan1](https://forum.ansible.com/u/Tzach_Livyatan1)\
**Post date:** [October 21, 2015, 4:11pm UTC](https://forum.ansible.com/t/extracting-inner-nested-elements-from-a-list-or-launching-ec2-instances-on-more-than-one-zone/19577/1 "2015-10-21T16:11:33Z")

</div>

Hello wise people of Ansible

An existing playbook I have launch EC instances as follow (simplify):

`

- name: Launch instances  
local\_action:  
module: ec2  
region: “{{region}}”  
instance\_type: “{{instance\_type}}”  
image: “{{image}}”  
count: “{{cluster\_nodes}}”  
register: ec2\_clusterEnter code here…  
`

I later use ec2\_cluster as:

`

local\_action: add\_host name={{item.public\_ip}} groupname=New  
with\_items: ec2\_cluster.instances

`

Now, I need to launch instances on more than one zone, so I added a dictionary with all the variables, and use the same operation with  
`  
“with\_dict: regions”

`

This works fine, but the ec2\_cluster format has change. It have all the data as before, but the instances are nested in  
ec2\_cluster.results[0].instances,

ec2\_cluster.results[1].instances

etc

My question (finally):  
What is the idiomatic way to extract the public ip of all the instances from all the regions?

Thanks  
Tzach

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan1](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tzach\_Livyatan1](https://forum.ansible.com/u/Tzach_Livyatan1)\
**Post date:** [October 21, 2015, 6:16pm UTC](https://forum.ansible.com/t/extracting-inner-nested-elements-from-a-list-or-launching-ec2-instances-on-more-than-one-zone/19577/2 "2015-10-21T18:16:17Z")

</div>

Answering my own question:

Good question Tzach 😉  
This is how you should do it:

`

local\_action: add\_host name={{item.1.public\_ip}} groupname=NewDB  
with\_subelements:

- ec2\_cluster.results
- instances

`

---

<div class="post-metadata">

**Author:** ![Tzach\_Livyatan1](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@Tzach\_Livyatan1](https://forum.ansible.com/u/Tzach_Livyatan1)\
**Post date:** [October 21, 2015, 6:17pm UTC](https://forum.ansible.com/t/extracting-inner-nested-elements-from-a-list-or-launching-ec2-instances-on-more-than-one-zone/19577/3 "2015-10-21T18:17:19Z")

</div>

posting again with right Indentation

`

local\_action: add\_host name={{item.1.public\_ip}} groupname=NewDB  
with\_subelements:

- ec2\_cluster.results
- instances

`
