# Processing registered result of ec2 with\_items

**URL:** <https://forum.ansible.com/t/processing-registered-result-of-ec2-with-items/16129>\
**Category:** Ansible Project\
**Created:** [September 5, 2014, 6:00pm UTC](https://forum.ansible.com/t/processing-registered-result-of-ec2-with-items/16129 "2014-09-05T18:00:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Van\_de\_Walle](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@Tim\_Van\_de\_Walle](https://forum.ansible.com/u/Tim_Van_de_Walle)\
**Post date:** [September 5, 2014, 6:00pm UTC](https://forum.ansible.com/t/processing-registered-result-of-ec2-with-items/16129/1 "2014-09-05T18:00:16Z")

</div>

I have a role that encapsulates creation of ec2 instances. It’s sole functionality is to create a specified number of ec2 instances, register them in a group and then wait for the SSH port to be available for those instances so that they can be further provisioned. For availability reasons we create instances in at a minimum of two availability zones.

Most of the configuration of the main play is listed below:  
`

- name: Launch image(s)  
local\_action:  
module: ec2  
region: “{{item.ec2\_region}}”  
zone: “{{item.ec2\_zone}}”  
instance\_type: “{{item.ec2\_instance\_type}}”  
image: “{{item.ec2\_image}}”  
key\_name: “{{item.ec2\_keypair}}”  
exact\_count: “{{item.ec2\_exact\_count}}”  
group: “{{item.ec2\_security\_groups}}”  
instance\_tags: “{{item.ec2\_tags}}”  
count\_tag: “{{item.ec2\_tags}}”  
vpc\_subnet\_id: “{{item.ec2\_vpc\_subnet\_id}}”  
instance\_profile\_name: “{{item.ec2\_instance\_profile\_name}}”  
wait: true  
register: ec2

- name: Add new servers to group for configuration  
local\_action: add\_host hostname={{item.public\_dns\_name}} groupname={{ec2\_groupname}}  
with\_items: ec2.instances  
when: ec2.changed

- name: Wait for the instances to boot by checking the ssh port  
local\_action: wait\_for host={{item.public\_dns\_name}} port=22 delay=60 timeout=320 state=started  
with\_items: ec2.instances  
when: ec2.changed

`

This works as expected, however this requires the calling playbook to call this role once for each ec2 definition and because we are launching into multiple availability zones there are always at least 2 ec2 definitions. I decided to pass a list of definitions to the task that launches the ec2 instances.

The new launch task looks like (notice the with\_items):  
`

- name: Launch image(s)  
local\_action:  
module: ec2  
region: “{{item.ec2\_region}}”  
zone: “{{item.ec2\_zone}}”  
instance\_type: “{{item.ec2\_instance\_type}}”  
image: “{{item.ec2\_image}}”  
key\_name: “{{item.ec2\_keypair}}”  
exact\_count: “{{item.ec2\_exact\_count}}”  
group: “{{item.ec2\_security\_groups}}”  
instance\_tags: “{{item.ec2\_tags}}”  
count\_tag: “{{item.ec2\_tags}}”  
vpc\_subnet\_id: “{{item.ec2\_vpc\_subnet\_id}}”  
instance\_profile\_name: “{{item.ec2\_instance\_profile\_name}}”  
wait: true  
register: ec2  
with\_items: ec2\_defs

`

The instances launch as expected, but problems arise when I try to use the registered result. The registered result (edited down a bit) looks like:

`  
“ec2”: {  
“changed”: true,  
“msg”: “All items completed”,  
“results”: [  
{  
“changed”: true,  
“instance\_ids”: [  
“i-85170f6a”  
],  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“hypervisor”: “xen”,  
“virtualization\_type”: “paravirtual”  
}  
],  
“invocation”: {  
“module\_args”: “”,  
“module\_name”: “ec2”  
},  
]  
},  
{  
“changed”: true,  
“instance\_ids”: [  
“i-9c18cf77”  
],  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“hypervisor”: “xen”,  
}  
],  
“invocation”: {  
“module\_args”: “”,  
“module\_name”: “ec2”  
},  
}  
]  
}

`

I need to be able to iterate through all of the instances list in each of the results and I can’t seem to find a loop construct that will make this happen.

This seems like a fairly common pattern, but perhaps I’m starting in the wrong place or it’s just a known limitation. Any thoughts on a different approach?

Tim

---

<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:** [February 11, 2015, 4:45pm UTC](https://forum.ansible.com/t/processing-registered-result-of-ec2-with-items/16129/2 "2015-02-11T16:45:07Z")

</div>

Hi,

Did you find a solution to this? Having a similar issue

J

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [February 12, 2015, 8:25am UTC](https://forum.ansible.com/t/processing-registered-result-of-ec2-with-items/16129/3 "2015-02-12T08:25:53Z")

</div>

You might want to try with\_sublements ([http://docs.ansible.com/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/playbooks_loops.html#looping-over-subelements)) - note the use of item.1 instead of just item:

---

<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:** [February 12, 2015, 10:20am UTC](https://forum.ansible.com/t/processing-registered-result-of-ec2-with-items/16129/4 "2015-02-12T10:20:36Z")

</div>

Hey,

Thanks for the info. That looks like what i’m looking for

Cheers
