# name of the task that calls a handler

**URL:** <https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934>\
**Category:** Ansible Project\
**Created:** [September 27, 2012, 3:57pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934 "2012-09-27T15:57:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [September 27, 2012, 3:57pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/1 "2012-09-27T15:57:44Z")

</div>

Is there a way to get the name of the task that calls a handler?

I was trying to make an generic service restart handler in line with:

tasks:

- name: **ntpd**  
action: copy src=${GITREPO}/${DIST}/$item dest=/$item owner=root group=root mode=0644  
with\_items:
- etc/ntp.conf
- etc/sysconfig/ntpd  
notify:
- restart\_service

handlers:

- name: restart\_service  
action: service name= **${name\_of\_task}** state=restarted enabled=yes

… or is there a better way to do it?

---

<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:** [September 27, 2012, 4:22pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/2 "2012-09-27T16:22:53Z")

</div>

It seems like what you want is almost like:

- restart\_service name=ntp

AND:

action: service name=$name

and a way to pass parameters to a notifier. This would need to add a  
handler for that combination if it did not exist when it got back that  
it had to notify that handler.

I'm open to patches that try to add that. (Not so much the magic  
variable though)

Scope may get a little confusing.

---

<div class="post-metadata">

**Author:** ![PJ1](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@PJ1](https://forum.ansible.com/u/PJ1)\
**Post date:** [September 28, 2012, 8:32am UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/3 "2012-09-28T08:32:22Z")

</div>

That would save a lot of unnecessary typing and extra code when adding a lot of services for sure.

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [September 11, 2013, 2:12pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/4 "2013-09-11T14:12:42Z")

</div>

Hello,

I could not find an answer/more recent answer on this thread. Is there a way to define a bunch of handlers via a with\_items ?

In role/myrole/handlers/main.yml

- name: restart service {{ item.name }}  
service: name={{ item.name }} state=restarted  
with\_items: service\_names

In role/myrole/vars/main.yml

service\_names:

- apache

In role/myrole/tasks/main.yml

…  
notify: restart service apache

but I get an error :  
ERROR: change handler (restart service apache) is not defined

I also tried  
notify: restart service name=apache

with

- name: restart service  
service: name={{ name }} state=restarted

but to no avail.

Am I missing something ?  
Is there a way in the ansible git version to define handlers in a loop or to parametrize handlers ?

---

<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:** [September 11, 2013, 6:36pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/5 "2013-09-11T18:36:11Z")

</div>

No, there isn’t a way to do this exactly as you say.

Each handler must have it’s own name, and the with\_items is triggered when the handler is run, it is not something that is used to define the handler multiple times.

You will want to define a handler for each service.

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [September 12, 2013, 12:55pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/6 "2013-09-12T12:55:07Z")

</div>

I have read past messages/tickets debating on parametrized handlers. I understand it would be acceptable to have :

notify: restart service name=apache

and a handler defined as :

- name: restart service  
service: name={{ name }} state=restarted

as long as the name: of the handler is fixed.

It is a feature that could get into ansible core if a patch is submitted ?

Thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 12, 2013, 1:00pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/7 "2013-09-12T13:00:34Z")

</div>

I’m trying to add this same feature, fyi, you need to dedupe name, the hard part that i’m having issues with is when you have with\_items on both sides.

---

<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:** [September 12, 2013, 5:50pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/8 "2013-09-12T17:50:28Z")

</div>

I’d be interested in seeing your proposed syntax.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 12, 2013, 6:43pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/9 "2013-09-12T18:43:37Z")

</div>

I started much like original poster “notify: restart service name=apache” which basically ended up in a notify[‘name’] = set() to which i started appending values, this is not too hard.

with\_items at source was pushing me to arrays, which i was having I was trying to dedupe by comparing with previous arrays in notify[‘items’] = , implied with successful items from the task.

Where i got stumped is trying to traverse the items array in a sane manner when service also had with\_items, got it working with a Cartesian product, but don’t think this is optimal. I was thinking of adding a group: intersect|product|etc that affects with\_items: but this also seems overtly complicated.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 12, 2013, 6:53pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/10 "2013-09-12T18:53:49Z")

</div>

If you want, push up what you have to your github branch and shoot us the link, we can take a look at it and maybe offer pointers.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 12, 2013, 7:01pm UTC](https://forum.ansible.com/t/name-of-the-task-that-calls-a-handler/11934/11 "2013-09-12T19:01:55Z")

</div>

I will soon, I just focused on getting other stuff off my list (acl anyone?)
