Files
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

43 lines
1.6 KiB
YAML

---
# Runs after the idempotence check and before verify. Record the daemon's PID, ban an address, change a jail setting so jail.local
# is rewritten and the handler fires, and let verify prove the daemon was reloaded, not restarted. The PID is the discriminator:
# a restart starts a new process, a reload does not. The ban alone would not tell them apart, because Fail2Ban restores unexpired
# bans from its database when a jail starts. The PID goes to a file because verify is a separate play.
- name: Change a jail setting while an address is banned
hosts: all
gather_facts: false
tasks:
- name: Read the unit state of the Fail2Ban daemon
ansible.builtin.systemd:
name: fail2ban
register: fail2ban_side_effect_unit
- name: Keep the main PID for the verify step
ansible.builtin.copy:
content: "{{ fail2ban_side_effect_unit.status.MainPID }}\n"
dest: /run/fail2ban-molecule-mainpid
mode: "0644"
# 198.51.100.0/24 is TEST-NET-2 (RFC 5737), outside the 192.0.2.0/24 the converge step ignores.
- name: Ban a documentation address on the sshd jail
ansible.builtin.command: fail2ban-client set sshd banip 198.51.100.77
changed_when: true
- name: Rewrite jail.local with a different maxretry
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
vars:
fail2ban_ignoreip:
- 127.0.0.1/8
- "::1"
- 192.0.2.0/24
fail2ban_jails:
- name: sshd
enabled: true
port: ssh
filter: sshd
backend: systemd
maxretry: 4
bantime: "1h"
findtime: "10m"