0

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

  • 1
    Have you tried to add the handler to the role's handlers? – Henrik Pingel Jul 8 at 9:02
  • @realtebo why the handlers is not handled inside the role? – c4f4t0r Jul 8 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. – Henrik Pingel Jul 8 at 9:46
  • What happened when you ran the playbook with the handler properly in the role? Please show the output. – Michael Hampton Jul 8 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. – Henrik Pingel Jul 8 at 10:40
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.

| improve this answer | |
  • Nothing changed ! – realtebo Jul 8 at 10:18
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.