# Chose random host

**URL:** <https://forum.ansible.com/t/chose-random-host/12826>\
**Category:** Ansible Project\
**Created:** [May 18, 2013, 1:05am UTC](https://forum.ansible.com/t/chose-random-host/12826 "2013-05-18T01:05:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curtis](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@Curtis](https://forum.ansible.com/u/Curtis)\
**Post date:** [May 18, 2013, 1:05am UTC](https://forum.ansible.com/t/chose-random-host/12826/1 "2013-05-18T01:05:56Z")

</div>

Hi All,

What I need to do is pick one of the ansible\_hosts group members randomly to run some cluster bootstrap code on it. I don’t care which one, but it can be only one. Any suggestions as to how to do that?

Thanks,  
Curtis.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 18, 2013, 5:27pm UTC](https://forum.ansible.com/t/chose-random-host/12826/2 "2013-05-18T17:27:32Z")

</div>

There’s presently no mechanism to do this.

Is there anything wrong with always picking the first one, perhaps?

---

<div class="post-metadata">

**Author:** ![Curtis](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@Curtis](https://forum.ansible.com/u/Curtis)\
**Post date:** [May 18, 2013, 9:12pm UTC](https://forum.ansible.com/t/chose-random-host/12826/3 "2013-05-18T21:12:50Z")

</div>

> There's presently no mechanism to do this.
> 
> Is there anything wrong with always picking the first one, perhaps?

That would work. Do you happen to know if there are any examples as to how  
I might go about doing that? 🙂

Thanks,  
Curtis.

---

<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:** [May 18, 2013, 9:46pm UTC](https://forum.ansible.com/t/chose-random-host/12826/4 "2013-05-18T21:46:00Z")

</div>

Sure thing, this is pretty straightforward.

Group names in host groups can take offsets, like so:

hosts: groupname[0]

And even:

# first five hosts

hosts: groupname[0-4]

So the first example is an easy way to target just the first host in a group.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 19, 2013, 12:11am UTC](https://forum.ansible.com/t/chose-random-host/12826/5 "2013-05-19T00:11:55Z")

</div>

​nice trick, didn't know pattern took this

Downside of this method, however, is that ​it forces you to repeat that for  
every group of clusternodes.  
At work we have many groups containing clusternodes. Almost all of those  
need some single action on the clusterlevel, and it would be neat to be  
able to say e.g. "create proxy configurations for all tomcat applications  
clusters in all environments"

we have many groups like "app" containg "app-dev" and "app-test" and  
"app-prod", and I would want to run a playbook for the proxy configuration  
on the master group containing \*all\* "app" groups.

I've played a little adapting group\_by to a group\_by\_pool.py plugin, where I  
loop through all the hosts groups, and pick those with the highest depth  
(1) to create a "pool" group. which now gives me this:

$ ansible -m group\_by\_pool mkros --list-hosts  
&nbsp;&nbsp;&nbsp;&nbsp;mkros-oe-1-mgt  
&nbsp;&nbsp;&nbsp;&nbsp;mkros-oe-2-mgt  
&nbsp;&nbsp;&nbsp;&nbsp;mkros-on-1-mgt  
&nbsp;&nbsp;&nbsp;&nbsp;mkros-on-2-mgt  
&nbsp;&nbsp;&nbsp;&nbsp;mkros-pr-1-mgt  
&nbsp;&nbsp;&nbsp;&nbsp;mkros-pr-2-mgt

$ ansible -m group\_by\_pool mkros -a key=POOL  
mkros-oe-1-mgt | success \>\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": true,  
&nbsp;&nbsp;&nbsp;&nbsp;"groups": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"POOL\_mkros-oe": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mkros-oe-1-mgt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"POOL\_mkros-on": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mkros-on-1-mgt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"POOL\_mkros-pr": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mkros-pr-1-mgt"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

​Would this be interesting?

&nbsp;&nbsp;&nbsp;Serge​

​(1) I'm sometimes seeing the same effect with depth I mentioned on another  
thread

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 19, 2013, 12:21am UTC](https://forum.ansible.com/t/chose-random-host/12826/6 "2013-05-19T00:21:16Z")

</div>

“Downside of this method, however, is that it forces you to repeat that for every group of clusternodes.”

Depends on what you are doing.

If you have a 50 node Riak cluster or a 100 node Hadoop or something, and you want to run something on only one node, that’s nothing to repeat.

Not sure if I understand what the group by pool does.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 19, 2013, 12:40am UTC](https://forum.ansible.com/t/chose-random-host/12826/7 "2013-05-19T00:40:25Z")

</div>

> "Downside of this method, however, is that it forces you to repeat that  
> for every group of clusternodes."
> 
> Depends on what you are doing.
> 
> If you have a 50 node Riak cluster or a 100 node Hadoop or something, and  
> you want to run something on only one node, that's nothing to repeat.

But then you'd ​​probably just have a situation where you only manage those  
nodes in 1 big group, as they are all equivalent.  
Say you have three environments, a 50 node development hadoop cluster, a 50  
node testing hadoop cluster and a 50 node production hadoop cluster (I  
don't know what hadoop is, so forgive me if that doesn't make sense.  
s/hadoop/apache/

You could then do:  
- hosts: hadoop-dev[0]  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- action: do something on the cluster  
- hosts: hadoop-test[0]  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- action: do something on the cluster  
- hosts: hadoop-prod[0]  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- action: do something on the cluster

Or:  
- hosts: hadoop  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- action: group\_by\_pool # this would group by hadoop-dev/test/prod and  
target just one node per new group  
- hosts: POOL-hadoop-\*  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- action: do something on the cluster

In my environment I have over 3 environments with each 100 clusters, so  
effectively over 300 clusters, for which I would need to do the same  
action, so that last solution with two plays would scale a lot better for  
me.

Not sure if I understand what the group by pool does.

> 

​It basically takes eacht child group, where 1 group is 1 cluster, and  
which has all the nodes for that cluster, and creates a new group  
containing just the first node of that original group.

That was just a quick idea and proof of concept, that seems to do what I  
might need. Not saying right now all the details are optimal though. I  
possibly should get a life on Saturday nights.

&nbsp;&nbsp;Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 19, 2013, 1:40pm UTC](https://forum.ansible.com/t/chose-random-host/12826/8 "2013-05-19T13:40:16Z")

</div>

I think you are conflating the problem.

He requested how to address one node out of a group, not how to manage lots of first nodes in lost of groups.

Then you’re going on into a long discussion about something he wasn’t talking about.

Let’s keep things simple, please 🙂

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 19, 2013, 5:33pm UTC](https://forum.ansible.com/t/chose-random-host/12826/9 "2013-05-19T17:33:50Z")

</div>

​The solution you gave for the initial question is a perfect fit.

I'm just telling I have the same problem, just on a bigger scale, where  
that solution doesn't fit in, and was thinking about how I could solve  
this.​ I'd thought this would be an interesting use case and this was  
merely FYI, asking if it would be of any interest to anyone.  
​

​ Serge​

---

<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:** [May 19, 2013, 6:09pm UTC](https://forum.ansible.com/t/chose-random-host/12826/10 "2013-05-19T18:09:03Z")

</div>

In your case, I would just make a group of all of the head nodes.

---

<div class="post-metadata">

**Author:** ![Rene\_Moser](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@Rene\_Moser](https://forum.ansible.com/u/Rene_Moser)\
**Post date:** [December 13, 2016, 4:43pm UTC](https://forum.ansible.com/t/chose-random-host/12826/11 "2016-12-13T16:43:29Z")

</div>

Better late than never

- hosts: localhost  
tasks:

- add\_host:  
name: “{{ item }}”  
groups: elected\_leader  
with\_random\_choice: “{{ groups[‘web’] }}”

- hosts: elected\_leader  
gather\_facts: no  
tasks:

- debug: var=inventory\_hostname
