# EC2 Query Module

**URL:** <https://forum.ansible.com/t/ec2-query-module/17742>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [March 13, 2015, 6:01pm UTC](https://forum.ansible.com/t/ec2-query-module/17742 "2015-03-13T18:01:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Jeff\_Gonzalez](https://forum.ansible.com/u/Jeff_Gonzalez)\
**Post date:** [March 13, 2015, 6:01pm UTC](https://forum.ansible.com/t/ec2-query-module/17742/1 "2015-03-13T18:01:38Z")

</div>

Is there an EC2 query module that allows you to look up various resources and associated metadata that can be registered as variables? I consistently seem to run across a problem using Ansible (I assume this is a lack of understanding or misuse of Ansible on my part) where I am doing a sequence of events like this:

1. Look up an AMI with some specific tags
2. Use said AMI to create ec2 instance(s)
3. Add created instance(s) to dynamic hostgroup
4. Provision instance(s) in dynamic hostgroup

I know how to achieve this, but my problem occurs if there is some sort of error in step #4. How do I re-assemble the state of steps 2 and 3, without running something like aws cli?

This is how I am doing an approximation of what I want, but I feel like I am doing something wrong. What I really want is to use registered variables, because those seem to be global to playbooks, where facts seem host specific.

---

<div class="post-metadata">

**Author:** ![WhileLoop](https://avatars.discourse-cdn.com/v4/letter/w/bc8723/32.png) [@WhileLoop](https://forum.ansible.com/u/WhileLoop)\
**Post date:** [March 30, 2015, 1:50am UTC](https://forum.ansible.com/t/ec2-query-module/17742/2 "2015-03-30T01:50:41Z")

</div>

Try this.

[https://github.com/edx/configuration/blob/master/playbooks/library/ec2\_lookup](https://github.com/edx/configuration/blob/master/playbooks/library/ec2_lookup)
