-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure_ssh.yml
66 lines (57 loc) · 1.92 KB
/
secure_ssh.yml
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
---
- name: Secure SSH by enforcing key-based authentication
hosts: remote_servers
become: yes
tasks:
- name: Validates if user "{{ new_user }}" exists
ansible.builtin.getent:
database: passwd
key: "{{ new_user }}"
- name: Check if authorized_keys file exists
stat:
path: "/home/{{ new_user }}/.ssh/authorized_keys"
register: authorized_keys_file
- name: Validate authorized_keys file is not empty
fail:
msg: "The authorized_keys file is empty for user {{ new_user }}!"
when: authorized_keys_file.stat.exists and authorized_keys_file.stat.size == 0
- name: Ensure the SSH configuration file is backed up
copy:
src: /etc/ssh/sshd_config
dest: /etc/ssh/sshd_config.bak
remote_src: yes
owner: root
group: root
mode: 0600
- name: Configure SSH to disable password authentication and enforce key-based login
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PasswordAuthentication'
line: 'PasswordAuthentication no'
state: present
backup: yes
- name: Ensure PermitRootLogin is disabled
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PermitRootLogin'
line: 'PermitRootLogin prohibit-password'
state: present
backup: yes
- name: Ensure PubkeyAuthentication is enabled
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?PubkeyAuthentication'
line: 'PubkeyAuthentication yes'
state: present
backup: yes
- name: Ensure ChallengeResponseAuthentication is disabled
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^#?ChallengeResponseAuthentication'
line: 'ChallengeResponseAuthentication no'
state: present
backup: yes
- name: Restart SSH service to apply changes
service:
name: ssh
state: restarted