fix(ufw, fail2ban): pass --check on a host without the package

Under --check the package task only reports a change, so on a fresh host
the tasks after it ran against a missing binary, /etc directory or unit
and failed. They now skip when the install would change in check mode.

fail2ban registers __fail2ban_install and guards its template, service,
config test, jail tasks and handler with the standard
`not ansible_check_mode or ... is not changed` guard. ufw registers
__ufw_install plus a command probe (ansible_check_mode is not set for a
play-level check_mode inside include_role), combined once in vars/main.yml
as __ufw_can_configure. flush_handlers is left unguarded: the handler it
runs is guarded itself.

A check-absent scenario per role purges the package, refreshes the APT
cache and runs only `check`, so each fix is tested; one scenario covering
both would make the ufw role a user of fail2ban and select the ufw and
traefik scenarios on every fail2ban change.

Fixes #478

Assisted-by: Claude Code
This commit is contained in:
Defame1297 committed 2026-11-29 16:39:45 +00:00
1 parent 0173f6f226
commit 924a0ea7a7
20 files changed
+234 -9

No files matched your search

+4 -2
View File
@@ -89,17 +89,19 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_
- Verify checks that the `sshd` jail does not read `/var/log/auth.log`, that `jail.local` holds the configured `ignoreip` in `[DEFAULT]` and none of the refused values, that each service jail has its own `jail.d` file and the refused one left no file, that `fail2ban-client -t` passes, and that the running daemon loaded `sshd`, `svc-a` and `svc-b` and applies the `ignoreip` network to both a base jail and a service jail.
- The side effect bans an address and rewrites `jail.local` with a new `maxretry`; verify checks the new value is active, the ban is still in force and the daemon PID is unchanged, so the change reloaded Fail2Ban instead of restarting it.
The scenario `molecule/check-absent/` runs on the same two platforms: prepare purges fail2ban and refreshes the APT cache, then `check` runs the main entry point and `service_jail` under `--check`, which must pass with fail2ban not installed. Nothing is applied.
Not covered: no failure is sent through a jail, so a ban triggered by `failregex` is not tested, and the reload behaviours read from the Fail2Ban source (see Notes) are not run.
## Notes
- Idempotent: yes.
- Check mode: `--check` previews a host where fail2ban is already installed; a play-level `check_mode: true` is not supported. On a host without fail2ban it fails at the configuration and service tasks: the package is not installed under `--check`, so `/etc/fail2ban`, `fail2ban-client` and the unit are missing.
- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host without fail2ban the preview reports the install and skips the configuration, the service and the jail tasks, which need `/etc/fail2ban`, `fail2ban-client` and the unit.
- `fail2ban_ignoreip` lives in the `[DEFAULT]` section of `jail.local`, so it also applies to the jails that `service_jail` writes to `jail.d`. The exemption only exists once the main entry point has written `jail.local` on that host: `service_jail` does not read `fail2ban_ignoreip`, so a host that uses only `service_jail` has none.
- `fail2ban_ignoreip` replaces an `ignoreip` set in `jail.conf` or `jail.d/*.conf`, which Fail2Ban reads before `jail.local`; a `jail.d/*.local` file is read after it and can still override it for one jail. Entries must not contain whitespace, `;` or `%` (a `;` after whitespace starts a comment in Fail2Ban's config files and a `%` is interpolation syntax).
- Prefer IP addresses and CIDR networks in `fail2ban_ignoreip`. Fail2Ban resolves a hostname when the jail starts, and a failed lookup leaves that entry ineffective with only a log warning. The validation of `fail2ban_ignoreip` runs under the `fail2ban_validate` tag, so `--tags fail2ban_configure` skips it.
- Changing `jail.local` or a service jail reloads Fail2Ban instead of restarting it. A restart stops every jail, which flushes its bans out of the firewall until they are restored from the database, and forgets failures counted toward `maxretry`; a reload applies the new settings to the running jails. When the whole role runs, the reload comes after `fail2ban-client -t`.
- What a reload does not do, compared with the restart it replaces (read from the Fail2Ban 1.1.0 source, not run here): it does not unban an address that a widened `fail2ban_ignoreip` now covers (only a restart skips ignored addresses when it restores bans, so run `fail2ban-client unban <address>` to release one at once, or wait for the ban to expire); it does not change bans that already exist when `bantime` changes; and it does not apply server-level settings (`loglevel`, `dbfile`, `socket`, `pidfile`), which this role does not write. A changed `backend` restarts that jail by itself.
- With `--tags fail2ban_configure` only, `fail2ban-client -t` does not run before the reload; the reload (`fail2ban-client reload` in the upstream unit) still rejects a bad configuration and leaves the running daemon alone.
- `fail2ban-client -t` runs on every invocation of both entry points, not only when a file changed, so a broken file fails every run until fixed. It also runs in check mode.
- `fail2ban-client -t` runs on every invocation of both entry points, not only when a file changed, so a broken file fails every run until fixed. It also runs in check mode, except on a host without fail2ban.
- `no_log` is not used: the role handles no secrets.