# Dynamic inventorying on an EC2 tag basis

**URL:** <https://forum.ansible.com/t/dynamic-inventorying-on-an-ec2-tag-basis/19897>\
**Category:** Ansible Project\
**Created:** [November 30, 2015, 1:12pm UTC](https://forum.ansible.com/t/dynamic-inventorying-on-an-ec2-tag-basis/19897 "2015-11-30T13:12:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Langhorn1](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Andrew\_Langhorn1](https://forum.ansible.com/u/Andrew_Langhorn1)\
**Post date:** [November 30, 2015, 1:12pm UTC](https://forum.ansible.com/t/dynamic-inventorying-on-an-ec2-tag-basis/19897/1 "2015-11-30T13:12:12Z")

</div>

Hello,

I’m using EC2 dynamic inventorying, and would like to create a new Ansible group to apply roles to by referencing machines tagged with a specific EC2 tag key/value pair.

So far, I have this in my inventory/hosts file:

> [roles]  
> ec2\_tag\_Name\_jenkins\_development

I’ve set inventory/hosts as the default hostfile in ansible.cfg, too, and when I run ./inventory/ec2.py (the script is from ansible/ansible on GitHub), I can see the tag exists with the IP address of the instance I want to apply my role to.

For some reason, ansible-playbook tries to resolve the tag name in the hostfile rather than look it up in the hostfile. That doesn’t seem like intended behaviour to me.

Can anyone spot anything I’ve done wrong?

I’m coming to Ansible from a Puppet background, so if I’ve left off anything massively basic, let me know 🙂

Thanks in advance!

Andrew

---

<div class="post-metadata">

**Author:** ![Adithya\_Khamithkar](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Adithya\_Khamithkar](https://forum.ansible.com/u/Adithya_Khamithkar)\
**Post date:** [November 30, 2015, 7:54pm UTC](https://forum.ansible.com/t/dynamic-inventorying-on-an-ec2-tag-basis/19897/2 "2015-11-30T19:54:31Z")

</div>

You can refer this  
[https://github.com/adithyakhamithkar/ansible/blob/master/roles/hostname/tasks/ec2-tags.yml](https://github.com/adithyakhamithkar/ansible/blob/master/roles/hostname/tasks/ec2-tags.yml)

This playbook can be used for dynamic tagging of your inventory on AWS.

---

<div class="post-metadata">

**Author:** ![Ivan\_S\_Freitas](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@Ivan\_S\_Freitas](https://forum.ansible.com/u/Ivan_S_Freitas)\
**Post date:** [November 30, 2015, 8:06pm UTC](https://forum.ansible.com/t/dynamic-inventorying-on-an-ec2-tag-basis/19897/3 "2015-11-30T20:06:52Z")

</div>

What happens is that ec2's dynamic inventory is evaluated after the  
normal files, so ec2\_tag\_Name\_jenkins\_development is not recognized as  
a group (or host). One workaround is to define a empty group with the  
same name as the tagged one (which will be populated by the dynamic  
inventory script before running playbooks) and include it inside the  
desired group with the 'children' directive. Example:

[ec2\_tag\_Name\_jenkins\_development]

[role:children]  
ec2\_tag\_Name\_jenkins\_development]

Note that there must be a blank line after the  
[ec2\_tag\_Name\_jenkins\_development].

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [November 30, 2015, 8:17pm UTC](https://forum.ansible.com/t/dynamic-inventorying-on-an-ec2-tag-basis/19897/4 "2015-11-30T20:17:12Z")

</div>

If you're looking to integrate dynamic and static inventories, you might  
also find [https://github.com/caredotcom/ansible-quartermaster](https://github.com/caredotcom/ansible-quartermaster) interesting  
and/or useful.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
