# Search for EC2 Instances by Tag

**URL:** <https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603>\
**Category:** Ansible Project\
**Created:** [July 3, 2014, 9:13pm UTC](https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603 "2014-07-03T21:13:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Chan](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Dan\_Chan](https://forum.ansible.com/u/Dan_Chan)\
**Post date:** [July 3, 2014, 9:13pm UTC](https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603/1 "2014-07-03T21:13:48Z")

</div>

I have a handful of existing EC2 instances with a specific tag that I would like to target and execute Ansible against. What is the best way to go about this?

I tried to use the ec2.py script for EC2 Dynamic Inventory, but ran into issues when trying to execute the script.

dchan $ chmod +x ec2.py  
dchan $ ansible -i ec2.py us-east-1 -m ping  
File “/ec2.py”, line 292  
region ‘’’  
^  
IndentationError: expected an indented block

Is there something I’m doing wrong with the ec2.py script? I’m also unsure about how to incorporate the script into my playbook.  
Are there any existing modules that I could utilize to solve my problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![Stuart\_Reynolds](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Stuart\_Reynolds](https://forum.ansible.com/u/Stuart_Reynolds)\
**Post date:** [July 3, 2014, 10:47pm UTC](https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603/2 "2014-07-03T22:47:10Z")

</div>

Sounds like a syntax error in ec2.py  
Python uses whitespace indentation as part of the syntax. Did you open it in an editor an add or remove an white space?

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [July 4, 2014, 5:19pm UTC](https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603/3 "2014-07-04T17:19:22Z")

</div>

Very strange, that’s right in the middle of a docstring. No reason for it to break like that.

What does the result of “/usr/bin/env python --version” say?

Regards,  
-scott

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 7, 2014, 7:23pm UTC](https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603/4 "2014-07-07T19:23:47Z")

</div>

Have you perhaps hand-edited (or someone has) ec2.py?

---

<div class="post-metadata">

**Author:** ![Dan\_Chan](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Dan\_Chan](https://forum.ansible.com/u/Dan_Chan)\
**Post date:** [July 11, 2014, 6:39pm UTC](https://forum.ansible.com/t/search-for-ec2-instances-by-tag/15603/5 "2014-07-11T18:39:38Z")

</div>

PEBKAC, it was a misconfiguration on my part. Script works like a charm!
