aboutsummaryrefslogtreecommitdiff
path: root/tasks/main.yml
blob: 74b6fbfbe3d3188f4b8947184620adbe76874656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
- name: Disable PasswordAuthentication
  vars:
    sshd_config_path: /etc/ssh/sshd_config
  tags:
    - ssh
  block:
    - name: Check if sshd_config exists
      ansible.builtin.stat:
        path: '{{ sshd_config_path }}'
      register: sshd_config_file

    - name: Copy initial sshd config
      when: not sshd_config_file.stat.exists
      ansible.builtin.copy:
        src: /usr/etc/ssh/sshd_config
        dest: '{{ sshd_config_path }}'
        remote_src: true
        mode: '640'

    - name: Disable PasswordAuthentication in sshd config
      ansible.builtin.lineinfile:
        path: '{{ sshd_config_path }}'
        regexp: "^[#]?\\s?PasswordAuthentication"
        line: "PasswordAuthentication no"
      register: disable_passwds

    - name: Restart service sshd
      when: disable_passwds.changed
      ansible.builtin.service:
        name: sshd
        state: restarted