I can envision a handler wanting to know for example which of its many possible “listen” strings it was notified by. But therein lies madness, as it could be notified by many different strings. As implemented (last time I looked), a notification results in adding a task to a host’s list of handlers to be run eventually - if it isn’t already on the list. There’s no record kept of any context per notification. Without that sort of metadata, I don’t see a way to implement “smart handlers”.
So lets step back a second and figure out just which problem you want to solve. True, there’s a lot of redundancy in the handlers you cited, so it does feel like there should be a more elegant way to express it. However, they don’t change often, and any one of them is simple enough to maintain.
What I find most compelling about your case is,“Another pile of them are there for the ‘reload’ state” because now you’ve got a slight difference between a couple of places to keep in sync. If this were C instead of YAML, you bet I’d use a preprocessor macro or three.
But this is YAML, and Jinja2, and the marvelous python freakenstein monster that is Ansible. So — and I’m not recommending this, but it is another way to handle it, which is what you asked for — you could use an ansible.builtin.template step to produce a “roles/dynarole/handlers/no-regrets.yml” that contains all your service handlers, then dynamically include that role so that (I think this is how it works) those handlers would then be available for notification from any subsequent tasks in that play. You could split the playbook into a play to generate the handlers files, then subsequent plays to dynamically include or statically import them. Or you could have a separate play/playbook with the sole purpose of generating the largely redundant handlers file that you only run when your table of desired handlers - or the template that consumes it - changes.
I can’t honestly argue that this is a good idea. But so far I haven’t come up with any other suggestions. It’s tempting to try it, if only to say, “Here’s a cool thing that isn’t at all wrong that I’ll never do again.”
in the task that triggers the handler, is there a way to register a variable and then do a debug in the playbook only when the handler condition is met?
another thought, if the task that triggered the handler is important to track create a specific handler for it, but let the rest just use a generic reload/restart etc…
I'm not saying Ansible should generally adopt this concept. I think,
in some cases such configuration might make the project robuster,
simpler, and easily extendable. FWIW, it's feasible to control some
projects by structured meta-data only https://ansible-config-light.readthedocs.io/en/latest/qsg.html