vm-ansible/roles/common/tasks/ssh-security-settings.yaml

47 lines
1.2 KiB
YAML
Raw Normal View History

---
- name: Disable password authentication
2023-03-05 19:00:38 +01:00
ansible.builtin.replace:
path: /etc/ssh/sshd_config
regexp: 'PasswordAuthentication yes'
replace: 'PasswordAuthentication no'
- name: Disable root authentication
2023-03-05 19:00:38 +01:00
ansible.builtin.replace:
path: /etc/ssh/sshd_config
regexp: '#PermitRootLogin prohibit-password'
replace: 'PermitRootLogin no'
- name: Disable X11 forwarding
2023-03-05 19:00:38 +01:00
ansible.builtin.replace:
path: /etc/ssh/sshd_config
regexp: 'X11Forwarding yes'
replace: 'X11Forwarding no'
- name: Explicitly only listen on ipv4
2023-03-05 19:00:38 +01:00
ansible.builtin.replace:
path: /etc/ssh/sshd_config
regexp: '#AddressFamily any'
replace: 'AddressFamily inet'
- name: Check if AllowUsers is defined
2023-03-05 19:00:38 +01:00
ansible.builtin.lineinfile:
state: absent
path: /etc/ssh/sshd_config
regexp: "^AllowUsers"
check_mode: true
changed_when: false
register: checkallowusers
- name: Define AllowUsers if undefined
2023-03-05 19:00:38 +01:00
ansible.builtin.lineinfile:
state: present
path: /etc/ssh/sshd_config
line: "AllowUsers tormakris ansible service-user"
when: checkallowusers.found == 0
2022-04-16 21:03:00 +02:00
- name: "Restart sshd"
2023-03-05 19:00:38 +01:00
ansible.builtin.service:
2022-04-16 21:03:00 +02:00
name: sshd
2022-04-16 21:05:42 +02:00
state: restarted
2022-04-16 19:55:29 +02:00
...