Write fail2ban_ignoreip as ignoreip in the [DEFAULT] section of jail.local, so it applies to every jail, the service jails in jail.d included. The default is Fail2Ban's own (127.0.0.1/8 and ::1). Entries are validated (no whitespace, \Z-anchored) because they are joined into one line. The first run after upgrading adds the [DEFAULT] section and restarts Fail2Ban once. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Q72rsMCgwaR1sbYZangwbp
76 lines
4.4 KiB
Markdown
76 lines
4.4 KiB
Markdown
# 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`](meta/argument_specs.yml), which is the source of truth.
|
|
|
|
## Supported platforms
|
|
|
|
Ubuntu 24.04 (noble), the image 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.
|
|
|
|
## 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 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. |
|
|
|
|
## Tags
|
|
|
|
Main entry point: `fail2ban` (everything), `fail2ban_install`, `fail2ban_validate`, `fail2ban_configure`, `fail2ban_service`.
|
|
|
|
`service_jail`: `fail2ban`, `fail2ban_install`, `fail2ban_service_jail`.
|
|
|
|
## Example play
|
|
|
|
```yaml
|
|
- hosts: pve_lxcs
|
|
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 }}"
|
|
```
|
|
|
|
## Notes
|
|
|
|
- Each service owns `jail.d/<name>.local`, so one play cannot overwrite another service's jail. `jail.local` only holds `fail2ban_jails`.
|
|
- `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 first run after upgrading to a release with it adds that section and restarts Fail2Ban once.
|
|
- Changing `jail.local` restarts Fail2Ban, which drops the in-memory ban list. Service jails only trigger a reload.
|
|
- `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_service_jail` and `fail2ban_service_filter` must be passed as a single templated expression so they stay mappings, not strings or lists.
|
|
- `no_log` is not used: the role handles no secrets.
|