# Handler how-to in roles

**URL:** <https://forum.ansible.com/t/handler-how-to-in-roles/16055>\
**Category:** Ansible Project\
**Created:** [August 27, 2014, 6:51pm UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055 "2014-08-27T18:51:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [August 27, 2014, 6:51pm UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/1 "2014-08-27T18:51:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 27, 2014, 7:09pm UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/2 "2014-08-27T19:09:47Z")

</div>

This should do it for you:

- hosts: role3  
tasks:
- wait\_for: host={{host}} port=22  
with\_items: servers\_to\_wait\_for

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [August 28, 2014, 5:33am UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/3 "2014-08-28T05:33:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 28, 2014, 9:00pm UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/4 "2014-08-28T21:00:12Z")

</div>

groups[‘groupname’]

is a list of all hosts in a group, so

with\_items: groups[‘groupname’]

will do it.

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [August 29, 2014, 4:53am UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/5 "2014-08-29T04:53:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 29, 2014, 11:47am UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/6 "2014-08-29T11:47:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [August 29, 2014, 1:25pm UTC](https://forum.ansible.com/t/handler-how-to-in-roles/16055/7 "2014-08-29T13:25:34Z")

</div>

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
