Michael Zietlow
Michael Zietlow

Reputation: 65

Ansible Handlers not working unless I include: them in my play?

I have a fresh install of Ansible 2.9.17 and everything works great except handlers :-/ For some reason my ~roles/myservice/handlers/main.yml are not loaded UNLESS I include: the handlers: after every hosts: line in my main.yml files.
example:

handlers:
- import_tasks: ../handlers/main.yml

I can do this but I find specifying the "same handler" again and again per host: entry annoying. This cant be the best practice of ansible to do it this way. Anyone else experience this issue before? There must be a config I'm missing...

My folder structure :

~/roles/myservice/
                 ├── handlers
                 │   └── main.yml
                 ├── tasks
                 └── main.yml

My roles/myservice/handlers/main.yml is:

- name: restart myservice
  service:
    name: myservice
    state: restarted
  become: yes
  listen: restart myservice

The roles/myservice/tasks/main.yml is:

- name: Installing myservice
  become: yes
  become_user: root
  yum:
    name: myservice.rpm
  notify:
    - restart myservice

But when I run the play:

$  ansible-playbook -i hosts roles/myservice/tasks/main.yml --limit myservice

PLAY [myservice] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************* ok: [myservice]

TASK [Install myservice.] ************************************************************************************************************************************************************************************** ok: [myservice]

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************* ok: [myservice]

TASK [Installing myservice] ******************************************************************************************************************************** ERROR! The requested handler 'restart myservice' was not found in either the main handlers list nor in the listening handlers list

Upvotes: 0

Views: 2056

Answers (1)

gary lopez
gary lopez

Reputation: 1954

You are executing the main.yml file as a playbook, you need to create a playbook that executes the role

- name: Playbook
  gather_facts: false
  hosts: all

  roles:
    - myservice

Upvotes: 3

Related Questions