# host selection based on host variables

**URL:** <https://forum.ansible.com/t/host-selection-based-on-host-variables/12187>\
**Category:** Ansible Project\
**Created:** [January 7, 2013, 8:56pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187 "2013-01-07T20:56:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pradeep\_Jindal](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Pradeep\_Jindal](https://forum.ansible.com/u/Pradeep_Jindal)\
**Post date:** [January 7, 2013, 8:56pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187/1 "2013-01-07T20:56:43Z")

</div>

Hey Guys,

Was just going through the ec2.py inventory plugin, and it seems like a pretty useful thing. Is it possible in ansible to select hosts to execute on based on the host vars? For example: “ansible instance\_type=m1.small ping” should ping all the m1.small instances in my inventory.

---

<div class="post-metadata">

**Author:** ![James\_Martin](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@James\_Martin](https://forum.ansible.com/u/James_Martin)\
**Post date:** [January 7, 2013, 9:26pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187/2 "2013-01-07T21:26:52Z")

</div>

Pardeep,

The ec2.py script could be modified to generate dynamic groups based  
on the instance\_type. Then you could run the play against that group.

- James

---

<div class="post-metadata">

**Author:** ![Pradeep\_Jindal](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Pradeep\_Jindal](https://forum.ansible.com/u/Pradeep_Jindal)\
**Post date:** [January 7, 2013, 9:43pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187/3 "2013-01-07T21:43:18Z")

</div>

Yep, that certainly is an option. But if it could be performed at the vars level then one could use “ohai” or “facter” generated data also. It seems conceptually in ansible vars are only supposed to be used in playbooks. Please correct me if I am wrong here.

---

<div class="post-metadata">

**Author:** ![Pradeep\_Jindal](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Pradeep\_Jindal](https://forum.ansible.com/u/Pradeep_Jindal)\
**Post date:** [January 7, 2013, 10:02pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187/4 "2013-01-07T22:02:41Z")

</div>

Just realised that what I am talking about here would require a core change in the way ansible executes modules on the remote machine. Sorry for my previous email.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 7, 2013, 10:02pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187/5 "2013-01-07T22:02:45Z")

</div>

Pradeep Jindal wrote:

> Hey Guys,
> 
> Was just going through the ec2.py inventory plugin, and it seems like a  
> pretty useful thing. Is it possible in ansible to select hosts to execute  
> on based on the host vars? For example: "ansible instance\_type=m1.small  
> ping" should ping all the m1.small instances in my inventory.

This is what group\_by is for, creating groups based on your variables.  
However, in this case it isn't necessary, as the ec2 inventory plugin already  
creates a type\_m1\_small group containing all of your m1.small instances.

Daniel

---

<div class="post-metadata">

**Author:** ![Pradeep\_Jindal](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Pradeep\_Jindal](https://forum.ansible.com/u/Pradeep_Jindal)\
**Post date:** [January 7, 2013, 10:41pm UTC](https://forum.ansible.com/t/host-selection-based-on-host-variables/12187/6 "2013-01-07T22:41:21Z")

</div>

Ah! alright. How can I use group\_by module from command line to “ping” all “x86\_64” hosts in my inventory? Or is it that I will have to write a playbook like [https://github.com/ansible/ansible/blob/devel/examples/playbooks/group\_by.yml](https://github.com/ansible/ansible/blob/devel/examples/playbooks/group_by.yml) for doing the same?
