Disable handlers from running

There is currently no variable within Ansible which lets you test which tags have been specified at runtime.

As you have discovered, handlers run regardless of assigned tags. The documentation does not make it clear whether this is a bug or intended behaviour.

Some options are:

  • Add another variable to the task and use this since handlers can still be conditional on the when: clause.

  • Have the handler include another file with the action and a tag assigned. The file will always be included, but the tag will make the action conditional.

  • Have a tagged action in the task which then sets a local variable for the handler. This conversion in task would allow tags still be used at runtime.


Here's an example of how you could use a variable to skip a handler:

$ cat test.yaml
---
- hosts: localhost
  tasks:
  - copy:
      content: "{{ ansible_date_time.epoch }}" # This will always trigger the handler.
      dest: /tmp/debug
    notify:
      - debug

  handlers:
  - name: debug
    debug:
      msg: Hello from the debug handler!
    when:
    - skip_handlers | default("false") == "false"

Normal use would look like this:

$ ansible-playbook test.yaml

And to skip the handlers:

$ ansible-playbook test.yaml -e skip_handlers=true

Tags:

Ansible