Permission denied (publickey) after running Ansible role to disable password login

Viewed 664

I have an Ansible playbook that does the following:

  1. I generate SSH keys for the remote server
  2. Then I disable root login and password authentication in the role ssh
  3. Finally, I set the password for the root account.

The 2) and 3) part of the playbook are shown below:

- hosts: new_servers
  become: yes

  tasks:
    - block:

      - name: Run ssh role to enable login using SSH keys and disable password login
        include_role:
          name: ssh

      - name: Set Password on root Account
        user:
          name: user
          password: "{{ user_password_hash }}"
          update_password: always

The ssh role has the following tasks/main.yml, taking inspiration from this link:

---

- name: Set SSH port
  lineinfile:
    dest: /etc/ssh/sshd_config
    regexp: "^Port\\s"
    line: "Port {{ ssh_port }}"
    state: present
  notify: Restart SSH

- name: Disable root SSH login
  lineinfile:
    dest: /etc/ssh/sshd_config
    regexp: "^PermitRootLogin"
    line: "PermitRootLogin no"
    state: present
  notify: Restart SSH

- name: Disable SSH password authentication
  lineinfile:
    dest: /etc/ssh/sshd_config
    regexp: "^PasswordAuthentication"
    line: "PasswordAuthentication no"
    state: present
  notify: Restart SSH

- name: run all handlers now so it doesn't fail later in provisioning playbook
  meta: flush_handlers

The handler Restart SSH is as follows:

---

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

When I run this playbook, it runs all the tasks of the ssh role, but then fails at the last step of running the handler that restarts the sshd service, presumably because password login is now disabled?

PLAY [new_servers] *********************************************************************************************************************************************************************************************************************

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

TASK [Run ssh role to enable login using SSH keys and disable password login] ******************************************************************************************************************************************************

TASK [ssh : Set SSH port] **********************************************************************************************************************************************************************************************************ok: [cm002] => changed=true
  backup: ''
  msg: line added

TASK [ssh : Disable root SSH login] ************************************************************************************************************************************************************************************************ok: [mauocmacst002] => changed=true
  backup: ''
  msg: line added

TASK [ssh : Disable SSH password authentication] ***********************************************************************************************************************************************************************************changed: [mauocmacst002] => changed=true
  backup: ''
  msg: line added

RUNNING HANDLER [ssh : Restart SSH] ************************************************************************************************************************************************************************************************fatal: [cm002]: FAILED! =>
  msg: 'Failed to connect to the host via ssh: user@cm002: Permission denied (publickey).'

NO MORE HOSTS LEFT ***************************************************************************************************************************************************************************************************************************

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
cm002              : ok=0   changed=3    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

How do I restart the sshd service which is needed to be done for the changes to go into effect?

0 Answers
Related