Files
Kamino/roles/fail2ban
Defame1297 234648919c
Build / build (pull_request) Successful in 27s
Molecule / molecule (pull_request) Successful in 4s
Lint / lint (pull_request) Successful in 1m21s
docs(roles): bring every role README to the fixed heading set
Every public role README has the headings Supported platforms, Requirements
and run order, Role variables, Entry points (when several), Variables set by
the role (when any), Tags, Example play, Tests and Notes, in that order, with
an "Idempotent" line in Notes. Role-specific sections move under Entry points
or Notes; nothing is removed. The internal _binary_update README keeps its own
headings and gains the platform and Idempotent lines.

Closes #136

Assisted-by: Claude Code:claude-opus-5-5
2026-11-25 17:24:27 +00:00
..

fail2ban

Installs Fail2Ban, writes /etc/fail2ban/jail.local from fail2ban_jails, ensures the service runs and verifies the enabled jails loaded. A second entry point, service_jail, lets a service role deploy its own jail and filter into jail.d. Variables and their types are defined in meta/argument_specs.yml, which is the source of truth.

Supported platforms

Ubuntu 24.04 (noble), the platform the molecule scenario runs on. The default sshd jail reads the systemd journal because sshd logs there on that release. The role installs python3-systemd, which backend = systemd jails need.

Ubuntu hosts only: the role targets the Ubuntu guests whose system_baseline profiles install fail2ban; Proxmox hosts are not targeted.

Requirements and run order

  • Use it by its fully qualified name, mortis.kamino.fail2ban.
  • No hard prerequisite. system_baseline also lists fail2ban among its packages, so it normally runs first; this role installs it regardless.
  • Main entry point: apply it after ufw in the play that hardens a host.
  • service_jail entry point: called by a service role such as gitea (include_role with tasks_from: service_jail) after it has installed the service.
  • A file named by a jail's logpath must exist when fail2ban-client -t runs, or the configuration test fails.
  • Role dependencies: none.

Role variables

Main entry point:

Variable Default Required Description
fail2ban_package_name fail2ban no Package name.
fail2ban_jails one sshd jail (systemd backend, maxretry 3, bantime 1h, findtime 10m) no Jails written to jail.local. Each needs name, enabled, filter, maxretry, bantime, findtime and logpath, journalmatch or backend: systemd.
fail2ban_ignoreip 127.0.0.1/8, ::1 no Addresses, networks or hostnames never banned, on every jail (written as ignoreip in [DEFAULT], so service jails in jail.d are covered). Add the machine that runs Ansible or a CI runner. An empty list exempts nothing, loopback included; entries hold no whitespace.
fail2ban_service enabled: true, state: started no Service enablement and state.
fail2ban_defaults maxretry: 5, bantime: 600, findtime: 600 no Fallbacks for those keys. Only used by the service_jail entry point; main-entry jails must set all three (tasks/validate.yml asserts them), so there the fallback only applies when validation is skipped, for example under --tags fail2ban_configure.

service_jail entry point:

Variable Default Required Description
fail2ban_service_name none yes Jail name and file name under filter.d and jail.d.
fail2ban_service_jail none yes Jail mapping; needs logpath or journalmatch.
fail2ban_service_filter none yes Filter mapping; needs failregex, optional ignoreregex.
fail2ban_package_name, fail2ban_defaults as above no Read from the role defaults.

Entry points

  • main: writes jail.local from fail2ban_jails and runs the service, as in the first play of the example.
  • service_jail: a service role's own jail and filter, see service_jail.

service_jail

It deploys the service's jail and filter into jail.d. Its inputs are in the second table under Role variables, and the second play of the example calls it.

  • Each service owns jail.d/<name>.local, so one play cannot overwrite another service's jail. jail.local only holds fail2ban_jails.
  • fail2ban_service_jail and fail2ban_service_filter must be passed as a single templated expression so they stay mappings, not strings or lists.

Tags

Main entry point: fail2ban (everything), fail2ban_install, fail2ban_validate, fail2ban_configure, fail2ban_service.

service_jail: fail2ban, fail2ban_install, fail2ban_service_jail.

Example play

- hosts: containers
  roles:
    - role: mortis.kamino.fail2ban
      vars:
        fail2ban_defaults:
          maxretry: 5
          bantime: 600
          findtime: 600

- hosts: gitea
  tasks:
    - name: Deploy a service jail
      ansible.builtin.include_role:
        name: mortis.kamino.fail2ban
        tasks_from: service_jail
      vars:
        fail2ban_service_name: gitea
        fail2ban_service_jail: "{{ gitea_fail2ban_jail }}"
        fail2ban_service_filter: "{{ gitea_fail2ban_filter }}"

Tests

The scenario molecule/default/ runs in CI only, on one platform: ${MOLECULE_IMAGE}, an Ubuntu 24.04 (noble) container. The sequence runs prepare, converge, idempotence, side effect and verify; it has no check-mode step.

  • Converge applies the main entry point with an extra fail2ban_ignoreip network and calls service_jail twice, one jail with a logpath and one with a journalmatch.
  • Converge also runs inputs that must be refused: a service filter without failregex, and a fail2ban_ignoreip entry with whitespace, a plain string instead of a list, and an entry starting with ;. The fail2ban_ignoreip refusals are matched on their message.
  • 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.

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.
  • 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.
  • no_log is not used: the role handles no secrets.