1

My playbook

---
- name: Nuovo server GV
  gather_facts: no
  hosts:
    - new-gv.vm
  roles:
    - ssh_configuration
  handlers:
    - name: Restart sshd
      service:
        name: sshd
        state: restarted

In the ssh_configuration tasks file there is this

- name: Setup alternate SSH port
  become: yes
  become_method: su
  become_user: root
  lineinfile:
    dest: "/etc/ssh/sshd_config"
    regexp: "^Port"
    line: "Port {{ configured_port }}"
  notify:
    - Restart sshd

The problem is that Restart sshd is NOT executed.

I tried and retried a lot of times a clean restart and I am sure: the addition of the "Port 1122" line is added into remote host file.

I ask you how to diagnose the problem, I cannot understand if handler is not fired or is broken for some reason.

I also tried adding

- name: Ensure SSH is reloaded if need be
  meta: flush_handlers

In the -vvvv mode I can see ONLY

META: ran handlers

But I expected a section "RUN HANDLER", if I am not remembering wrong

I'm sure this is not an Ansible Bug, it's only due to my inexperience

6
  • 1
    Have you tried to add the handler to the role's handlers? Jul 8, 2020 at 9:02
  • @realtebo why the handlers is not handled inside the role?
    – c4f4t0r
    Jul 8, 2020 at 9:06
  • 1
    ah, sorry for that, misread that on my side. You can use handlers from a role in a play but not the other way. Jul 8, 2020 at 9:46
  • What happened when you ran the playbook with the handler properly in the role? Please show the output. Jul 8, 2020 at 10:34
  • @realtebo Please remove the include_tasks step. It is not needed. I think you are mixing up scopes here. What you are trying is to use a handler from a play in a role, which won't work. However you can use a handler from a role in a play where you are using that role. Jul 8, 2020 at 10:40

2 Answers 2

1

The problem here is that the handler is defined in the playbook when it should have been defined in the role. Place

- name: Restart sshd
  service:
    name: sshd
    state: restarted

inside roles/ssh_configuration/handlers/main.yml and it should work.

1
  • Nothing changed !
    – realtebo
    Jul 8, 2020 at 10:18
1

I removed handler definition from play book.

I created /roles/ssh_configuration/handlers/main.yml:

- name: Restart sshd
  service:
    name: sshd
    state: restarted

Changed playbook in

---
- name: Nuovo server GV
  gather_facts: no
  hosts:
    - new-gv.vm
  roles:
    - ssh_configuration
  handlers:
    - include_tasks: roles/ssh_configuration/handlers/main.yml
      

this works

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .