main.yml 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. notify: reload ssh
  18. - name: Allow SSH through UFW
  19. community.general.ufw:
  20. rule: allow
  21. port: "{{ ssh_port }}"
  22. proto: tcp
  23. - name: Allow role-specific ports through UFW
  24. community.general.ufw:
  25. rule: allow
  26. port: "{{ item }}"
  27. proto: tcp
  28. loop: "{{ allowed_ports | default([]) }}"
  29. - name: Enable UFW
  30. community.general.ufw:
  31. state: enabled
  32. policy: deny
  33. direction: incoming
  34. - name: Configure fail2ban SSH jail
  35. ansible.builtin.copy:
  36. dest: /etc/fail2ban/jail.local
  37. content: |
  38. [sshd]
  39. enabled = true
  40. port = {{ ssh_port }}
  41. maxretry = 5
  42. bantime = 3600
  43. findtime = 600
  44. owner: root
  45. group: root
  46. mode: "0644"
  47. notify: restart fail2ban
  48. - name: Enable and start fail2ban
  49. ansible.builtin.systemd:
  50. name: fail2ban
  51. enabled: yes
  52. state: started
  53. - name: Configure unattended-upgrades
  54. ansible.builtin.copy:
  55. dest: /etc/apt/apt.conf.d/20auto-upgrades
  56. content: |
  57. APT::Periodic::Update-Package-Lists "1";
  58. APT::Periodic::Unattended-Upgrade "1";
  59. owner: root
  60. group: root
  61. mode: "0644"