deploy-base/roles/hardening/tasks/main.yml
2025-07-27 12:57:13 -05:00

88 lines
2.6 KiB
YAML

---
- name: Include SELinux package tasks for EL (CentOS/RHEL)
ansible.builtin.include_tasks: centos-selinux.yml
tags: selinux
when: (ansible_distribution in ["CentOS" , "Red Hat Enterprise Linux", "RedHat"])
- name: Include SELinux package tasks for Fedora (non-atomic)
ansible.builtin.include_tasks: fedora-selinux.yml
tags: selinux
when: (ansible_distribution in ["Fedora"] and not ansible_local.os.is_atomic)
- name: Ensure firewalld is installed
become: true
when: ansible_os_family in ['RedHat'] # Ubuntu has shown packaging issues with this and *tables in particular
ansible.builtin.package:
name: firewalld
state: present
- name: Enable firewalld
become: true
when: ansible_os_family in ['RedHat']
ansible.builtin.service:
name: firewalld
state: started
enabled: true
- name: Harden sshd
tags: harden_sshd
become: true
block:
- name: "SSH: disable password auth"
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: "^PasswordAuthentication"
line: "PasswordAuthentication no"
validate: '/usr/sbin/sshd -t -f %s'
notify: restart sshd
- name: "SSH: config custom port"
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^Port '
line: "Port {{ hardened_ssh_port }}"
insertbefore: "(^|#)AddressFamily.*"
validate: '/usr/sbin/sshd -t -f %s'
when: (hardened_ssh_port is defined)
notify: restart sshd
- name: "Only allow root logins with keys"
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin '
line: 'PermitRootLogin prohibit-password'
validate: '/usr/sbin/sshd -t -f %s'
notify: restart sshd
- name: "Disallow keyboard interactive auth to address some PAM edge cases"
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^KbdInteractiveAuthentication '
line: 'KbdInteractiveAuthentication no'
validate: '/usr/sbin/sshd -t -f %s'
notify: restart sshd
- name: "SELinux grant for custom SSH port ({{ hardened_ssh_port }})"
become: true
community.general.seport:
ports: "{{ hardened_ssh_port }}"
proto: tcp
setype: ssh_port_t
state: present
when:
- hardened_ssh_port is defined
- ansible_selinux is defined
- ansible_selinux.status == 'enabled'
tags: selinux
- name: "Firewalld: grant access to custom SSH port"
become: true
ansible.posix.firewalld:
port: "{{ hardened_ssh_port }}/tcp"
permanent: true
immediate: true
state: enabled
when:
- hardened_ssh_port is defined
- ansible_os_family in ['RedHat']