Chose random host

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.

There’s presently no mechanism to do this.

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

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

Thanks,
Curtis.

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.

​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
    mkros-oe-1-mgt
    mkros-oe-2-mgt
    mkros-on-1-mgt
    mkros-on-2-mgt
    mkros-pr-1-mgt
    mkros-pr-2-mgt

$ ansible -m group_by_pool mkros -a key=POOL
mkros-oe-1-mgt | success >> {
    "changed": true,
    "groups": {
        "POOL_mkros-oe": [
            "mkros-oe-1-mgt"
        ],
        "POOL_mkros-on": [
            "mkros-on-1-mgt"
        ],
        "POOL_mkros-pr": [
            "mkros-pr-1-mgt"
        ]
    }
}

​Would this be interesting?

   Serge​

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

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

"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]
  tasks:
  - action: do something on the cluster
- hosts: hadoop-test[0]
  tasks:
  - action: do something on the cluster
- hosts: hadoop-prod[0]
  tasks:
  - action: do something on the cluster

Or:
- hosts: hadoop
  tasks:
  - action: group_by_pool # this would group by hadoop-dev/test/prod and
target just one node per new group
- hosts: POOL-hadoop-*
  tasks:
  - 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.

  Serge

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

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

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

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