Chose random host

2,416 views
Skip to first unread message

Curtis

unread,
May 17, 2013, 9:05:56 PM5/17/13
to ansible...@googlegroups.com
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.

--
Twitter: @serverascode
Blog: serverascode.com

Michael DeHaan

unread,
May 18, 2013, 1:27:32 PM5/18/13
to ansible...@googlegroups.com
There's presently no mechanism to do this.

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




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Curtis

unread,
May 18, 2013, 5:12:50 PM5/18/13
to ansible...@googlegroups.com
On Sat, May 18, 2013 at 11:27 AM, Michael DeHaan <mic...@ansibleworks.com> wrote:
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.
 




On Fri, May 17, 2013 at 9:05 PM, Curtis <server...@gmail.com> wrote:
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.

--
Twitter: @serverascode
Blog: serverascode.com

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Twitter: @serverascode
Blog: serverascode.com

Michael DeHaan

unread,
May 18, 2013, 5:46:00 PM5/18/13
to ansible...@googlegroups.com
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.






Serge van Ginderachter

unread,
May 18, 2013, 8:11:55 PM5/18/13
to ansible...@googlegroups.com

On 18 May 2013 23:46, Michael DeHaan <michael...@gmail.com> wrote:
Group names in host groups can take offsets, like so:

hosts:  groupname[0]

​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

Michael DeHaan

unread,
May 18, 2013, 8:21:16 PM5/18/13
to ansible...@googlegroups.com
"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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Serge van Ginderachter

unread,
May 18, 2013, 8:40:25 PM5/18/13
to ansible...@googlegroups.com
On 19 May 2013 02:21, Michael DeHaan <mic...@ansibleworks.com> wrote:
"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


Michael DeHaan

unread,
May 19, 2013, 9:40:16 AM5/19/13
to ansible...@googlegroups.com
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 :)




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Serge van Ginderachter

unread,
May 19, 2013, 1:33:50 PM5/19/13
to ansible...@googlegroups.com

On 19 May 2013 15:40, Michael DeHaan <mic...@ansibleworks.com> wrote:
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.

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

Michael DeHaan

unread,
May 19, 2013, 2:09:03 PM5/19/13
to ansible...@googlegroups.com
In your case, I would just make a group of all of the head nodes.




René Moser

unread,
Dec 13, 2016, 11:43:29 AM12/13/16
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages