Dynamic inventorying on an EC2 tag basis

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 :slight_smile:

Thanks in advance!

Andrew

You can refer this
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.

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].

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

                                      -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.