main.yml 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ---
  2. - name: Update apt cache
  3. ansible.builtin.apt:
  4. update_cache: yes
  5. cache_valid_time: 3600
  6. - name: Install base packages
  7. ansible.builtin.apt:
  8. name: "{{ base_packages }}"
  9. state: present
  10. - name: Deploy SSH hardening drop-in
  11. ansible.builtin.template:
  12. src: sshd-hardening.conf.j2
  13. dest: /etc/ssh/sshd_config.d/99-hardening.conf
  14. owner: root
  15. group: root
  16. mode: "0644"
  17. validate: "sshd -t -f /etc/ssh/sshd_config"
  18. notify: restart sshd
  19. - name: Allow SSH through UFW
  20. community.general.ufw:
  21. rule: allow
  22. port: "{{ ssh_port }}"
  23. proto: tcp
  24. - name: Allow role-specific ports through UFW
  25. community.general.ufw:
  26. rule: allow
  27. port: "{{ item }}"
  28. proto: tcp
  29. loop: "{{ allowed_ports | default([]) }}"
  30. - name: Enable UFW
  31. community.general.ufw:
  32. state: enabled
  33. policy: deny
  34. direction: incoming
  35. - name: Configure fail2ban SSH jail
  36. ansible.builtin.copy:
  37. dest: /etc/fail2ban/jail.local
  38. content: |
  39. [sshd]
  40. enabled = true
  41. port = {{ ssh_port }}
  42. maxretry = 5
  43. bantime = 3600
  44. findtime = 600
  45. owner: root
  46. group: root
  47. mode: "0644"
  48. notify: restart fail2ban
  49. - name: Enable and start fail2ban
  50. ansible.builtin.systemd:
  51. name: fail2ban
  52. enabled: yes
  53. state: started
  54. - name: Configure unattended-upgrades
  55. ansible.builtin.copy:
  56. dest: /etc/apt/apt.conf.d/20auto-upgrades
  57. content: |
  58. APT::Periodic::Update-Package-Lists "1";
  59. APT::Periodic::Unattended-Upgrade "1";
  60. owner: root
  61. group: root
  62. mode: "0644"