user140547
user140547

Reputation: 8200

Ansible - how to use include_role in handler

Given the following playbook, it does not work to call include_role with a dummy role foo in a handler. According to the doc:

Handlers are lists of tasks, not really any different from regular tasks, that are referenced by a globally unique name, and are notified by notifiers.

However, it does not seem to work when using include_role (and Ansible 2.4.1). Is there any workaround or other syntax to get this running?

- name: test roles
  hosts: localhost
  gather_facts: false

  tasks:

  - debug:
      msg: "call handler"
    changed_when: true
    notify: "bar"

  handlers:

   - name: "bar handler"
     include_role:
       name: foo
     listen: "bar"

Output:

[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
[WARNING]: Ignoring invalid attribute: listen


PLAYBOOK: playbook.yml *****************************************************************************
1 plays in playbook.yml

PLAY [test roles] *****************************************************************************
ERROR! Unexpected Exception, this is probably a bug: 'IncludeRole' object has no attribute 'listen'

Upvotes: 3

Views: 3828

Answers (1)

PicoutputCls
PicoutputCls

Reputation: 1579

It looks like the issue you are describing currently has a open issue associated with it on the Ansible git repository: include_role doesn't work from handlers

At the moment:

When the include_role module is used within an handler, the calling role fails, before knowing if the handler will be called.

Upvotes: 6

Related Questions