---
- name: Ensure log directory exists
  ansible.builtin.file:
    path: /var/log
    state: directory
    mode: '0755'
  become: yes

- name: Install fail2ban
  ansible.builtin.apt:
    name: fail2ban
    state: present
    update_cache: yes
  become: yes

- name: Template fail2ban jail.local configuration
  ansible.builtin.template:
    src: jail.local.j2
    dest: /etc/fail2ban/jail.local
    owner: root
    group: root
    mode: '0644'
  become: yes
  notify: Restart fail2ban

- name: Ensure fail2ban service is started and enabled
  ansible.builtin.service:
    name: fail2ban
    state: started
    enabled: yes
  become: yes
