# Dynamic hosts list in playbooks?

**URL:** <https://forum.ansible.com/t/dynamic-hosts-list-in-playbooks/11971>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [October 11, 2012, 6:30pm UTC](https://forum.ansible.com/t/dynamic-hosts-list-in-playbooks/11971 "2012-10-11T18:30:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [October 11, 2012, 6:30pm UTC](https://forum.ansible.com/t/dynamic-hosts-list-in-playbooks/11971/1 "2012-10-11T18:30:43Z")

</div>

FWIW, I wrote code one day that allows this (14 lines!)  
Check out [https://github.com/jhoekx/ansible/commits/discover-groups](https://github.com/jhoekx/ansible/commits/discover-groups)

Basically, you just write a module that's similar to a custom facts  
module, but instead of returning ansible\_facts, you return a list of  
groups in ansible\_groups.

I didn't send a pull request for this because of the brainstorming Dag  
mentioned and I'm not sure anymore of the right solution. This part is  
simple and powerful though. If it's acceptable, I'm happy to send it  
in.

Greetings,

Jeroen

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 11, 2012, 8:11pm UTC](https://forum.ansible.com/t/dynamic-hosts-list-in-playbooks/11971/2 "2012-10-11T20:11:06Z")

</div>

The idea that we could scan hosts in one play and partition them based  
on facts is appealing. I'd like to see some syntax that auto  
partitions them automatically based on like facts instead though.  
Think about automatically making a group of all my FreeBSD hosts, etc.

This directly relates to the EC2 discovery question posed earlier.

Something like a action\_plugin called 'group\_based\_on'

hosts: all

tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- ...  
&nbsp;&nbsp;&nbsp;&nbsp;- group\_based\_on: ansible\_os\_type

And for everyone who responded:

{ ansible\_os\_type: 'BeOS' }

It would auto spawn a group named 'ansible\_os\_type\_BeOS'

This could easily partition your hosts based on the value of any  
arbitrary command, including register variables.

Make it so, #1?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [October 11, 2012, 8:28pm UTC](https://forum.ansible.com/t/dynamic-hosts-list-in-playbooks/11971/3 "2012-10-11T20:28:01Z")

</div>

I see offhand my proposal is less interesting if you have 50 playbooks  
and a few masters and you don't know which ones you launch.

I think both are potentially useful, for very different reasons.
