Files
Kamino/roles/fail2ban/tasks/service.yml
T
Dragonish e0e9fa1b17
Build / build (pull_request) Successful in 16s
Molecule / molecule (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 1m23s
feat(fail2ban): reload instead of restart when jail.local changes
The main entry point notified a restart. A restart stops every jail, which
flushes its bans out of the firewall until they are restored from Fail2Ban's
database when the jail starts again, and forgets failures counted toward
maxretry. A reload applies the new settings to the running jails. service_jail
already reloads.

The Restart Fail2Ban handler is removed: nothing notifies it any more, and a
handler name is not part of the role's interface (#257). A new side_effect.yml
bans an address, records the daemon's PID and rewrites jail.local; verify
asserts the new maxretry is active, the ban is in force and the PID is
unchanged (the PID is what separates a reload from a restart, since bans are
restored from the database).

Assisted-by: Claude Code
2026-11-23 20:01:07 +00:00

37 lines
1.2 KiB
YAML

---
# -t loads every jail, so a jail that cannot start (missing logpath, bad
# backend) fails here. -d only dumps the config and treats those as warnings.
- name: Test Fail2Ban configuration
become: true
ansible.builtin.command: fail2ban-client -t
register: fail2ban_test
failed_when: fail2ban_test.rc != 0
changed_when: false
check_mode: false
- name: Ensure Fail2Ban service is running
become: true
ansible.builtin.systemd:
name: fail2ban
enabled: "{{ fail2ban_service.enabled | default(true) }}"
state: "{{ fail2ban_service.state | default('started') }}"
# Handlers run at the end of the play; flush so the reload happens after the
# configuration test above and before the jail check below.
- name: Apply pending Fail2Ban handlers
ansible.builtin.meta: flush_handlers
- name: Verify enabled jails are loaded
become: true
ansible.builtin.command: "fail2ban-client status {{ item.name }}"
loop: "{{ fail2ban_jails | selectattr('enabled') }}"
loop_control:
label: "{{ item.name }}"
register: fail2ban_jail_status
until: fail2ban_jail_status.rc == 0
retries: 5
delay: 2
changed_when: false
check_mode: false
when: fail2ban_service.state | default('started') == 'started'