Handler how-to in roles

56 views
Skip to first unread message

Mikael Sandström

unread,
Aug 27, 2014, 2:51:41 PM8/27/14
to ansible...@googlegroups.com
Hi,

I've got a play that consists of a number of roles, like so:

 - name: Ble
   hosts
: hosts
   sudo
: yes
   roles
:
     
- role1
     
- role2
     
- role3

In role2, I've got a handler that reboots the hosts and I need role3 to wait for the hosts to come up before starting (or role2 to somehow wait before exiting), and I'm not sure how to do this.
I know I have to use the 'wait_for' module, but I'm not sure where to put it. Should (can?) I put in the handler or should it be at the start of role3 or should I just skip the handler, insert a reboot + wait_for at the end of role2?

Any help appreciated.

/M

Michael DeHaan

unread,
Aug 27, 2014, 3:09:50 PM8/27/14
to ansible...@googlegroups.com
This should do it for you:

- hosts: role3
  tasks:
     - wait_for: host={{host}} port=22
        with_items: servers_to_wait_for



--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6d99be91-bbc8-43e0-85a5-84a45908c7bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikael Sandström

unread,
Aug 28, 2014, 1:33:02 AM8/28/14
to ansible...@googlegroups.com
Thanks, I'll try that.
So I guess I can somehow reference the hostgroup 'hosts' (which was a stupid name even for an example...) in the with_items section then, since the number of servers may change?

/M

Michael DeHaan

unread,
Aug 28, 2014, 5:00:15 PM8/28/14
to ansible...@googlegroups.com
groups['groupname']

is a list of all hosts in a group, so

with_items: groups['groupname'] 

will do it.





/M

--
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.
To post to this group, send email to ansible...@googlegroups.com.

Mikael Sandström

unread,
Aug 29, 2014, 12:53:34 AM8/29/14
to ansible...@googlegroups.com
Awesome, thanks!

So given:
[dbservers]
Node1
Node2

Is there a way to (in the play) get info on which group the play is running against?
I.e something that will tell me 'dbservers'?
Or is that something I have pass in as a variable?

/M

Michael DeHaan

unread,
Aug 29, 2014, 7:47:57 AM8/29/14
to ansible...@googlegroups.com
"Is there a way to (in the play) get info on which group the play is running against?"

In recent versions, should you need this, the variable "play_host" contains the list of all hosts currently being targetted.

There is no variable that gives you the host specifier, which is largely ok, because you also have no function that would resolve it.

I'm curious as to why it would be needed.





/M

--
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.
To post to this group, send email to ansible...@googlegroups.com.

Mikael Sandström

unread,
Aug 29, 2014, 9:25:34 AM8/29/14
to ansible...@googlegroups.com
Ok,

In this case its because [cluster1] makes up one cluster of dbservers and [cluster2] is another cluster, and those identifiers are used as the cluster-name for each config. But those can easily be passed in as a parameter so its not really a problem.

I just ran a few tests and noticed that the variable 'group_names' does contains the hostgroup [cluster1]

  - name: test
    debug
: msg="Running against hostgroup  {{ group_names }}  "
    with_items
:  groups['cluster1']



Output (snipped):
ok
: [node1] => (item=node1) => {
   
"item": "node1",
   
"msg": "You are running against hostgroup  ['cluster1']  "
}
ok
: [node2] => (item=node1) => {
   
"item": "node1",
   
"msg": "You are running against hostgroup  ['cluster1']  "
....


Anyway, thanks for the help.

/M
Reply all
Reply to author
Forward
0 new messages