# Creating subgroup of ec2 dynamic inventory

**URL:** <https://forum.ansible.com/t/creating-subgroup-of-ec2-dynamic-inventory/15196>\
**Category:** Ansible Project\
**Created:** [May 8, 2014, 6:32am UTC](https://forum.ansible.com/t/creating-subgroup-of-ec2-dynamic-inventory/15196 "2014-05-08T06:32:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [May 8, 2014, 6:32am UTC](https://forum.ansible.com/t/creating-subgroup-of-ec2-dynamic-inventory/15196/1 "2014-05-08T06:32:53Z")

</div>

Hi all,

I have the following playbook trying to create a subgroup out of my ec2 dynamic inventory with instances matching given tags:

---

<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:** [May 9, 2014, 11:56pm UTC](https://forum.ansible.com/t/creating-subgroup-of-ec2-dynamic-inventory/15196/2 "2014-05-09T23:56:37Z")

</div>

A few things to cleanup first:

" - add\_host: hostname={{ hostvars[inventory\_hostname][‘ec2\_publicIp’] }} groupname=ec2hosts server\_name={{ hostvars[inventory\_hostname][‘ec2\_public\_dns\_name’] }}"

When you get ginormous variables like this, slow down, and define useful shortcuts in your “vars” section, like this:

vars:  
ec2ip: “{{ hostvars[inventory\_hostname][‘ec2\_publicIp’] }}”

This allows cleaner playbooks like:

" - add\_host: hostname={{ ec2ip }} groupname=ec2hosts server\_name={{ ec2dns }}"

etc

That’s unrelated though.

Also this is overkill:

when: hostvars[inventory\_hostname][‘ec2\_region’] == ‘ap-southeast-2’

Could have just been:

when: “ec2\_region == ‘ap-southeast-2’”

Ansible likes it simple.

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [May 12, 2014, 3:08am UTC](https://forum.ansible.com/t/creating-subgroup-of-ec2-dynamic-inventory/15196/3 "2014-05-12T03:08:48Z")

</div>

Thanks Michael your suggestions noted. I did some more testing and looks like only one single instance, assuming the last instance, from the inventory is being added to the group, and this is without any conditions. Very simple playbook example:

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [December 24, 2014, 4:57am UTC](https://forum.ansible.com/t/creating-subgroup-of-ec2-dynamic-inventory/15196/4 "2014-12-24T04:57:36Z")

</div>

Just in case someone else had similar problem, the solution was to add “serial: 1” to the playbook otherwise the execution is parallel thus only the last host preprocessed ends up in the group.

Simple example /etc/ansible/hosts-group inventory:  
[group-tomcat]  
10.22.0.43 server\_name=app11 ec2\_private\_dns\_name=ip-10-22-0-43  
10.22.10.35 server\_name=app21 ec2\_private\_dns\_name=ip-10-22-10-35

Simple test33.yml playbook:
