name of the task that calls a handler

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?

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.

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

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 ?

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.

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

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.

I’d be interested in seeing your proposed syntax.

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.

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.

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