feat(fail2ban): add fail2ban_ignoreip to exempt trusted addresses #156

Merged
Defame1297 merged 6 commits from feat/fail2ban-ignoreip into main 2026-11-15 17:31:40 +00:00
Collaborator

Closes #155

What

fail2ban_ignoreip: a list of addresses or networks written as ignoreip in a new [DEFAULT] section of jail.local. The default is Fail2Ban's own (127.0.0.1/8, ::1), so nothing changes unless a consumer sets it.

Why

The role had no way to keep a trusted address from being banned. A mistyped key or a looping health check from the machine that runs Ansible, or from a CI runner, can get that address banned and lock automation out of the host. [DEFAULT] covers both the jails of fail2ban_jails and the service jails that other roles add with tasks_from: service_jail (written to jail.d); a per-jail ignoreip would need repeating on every jail and would miss service jails.

Changes

  • defaults/main.yml, templates/jail.local.j2: the variable and the [DEFAULT] block.
  • tasks/validate.yml: the value must be a list of non-empty strings (argument_specs accepts a plain string for a list option and leaves it a string, and accepts numbers and null in the list, so the type is checked here first). Entries must hold no whitespace (they are joined with spaces), no ; (after whitespace it starts an inline comment in Fail2Ban's config files, which drops the rest of the line without an error) and no % (interpolation syntax, which fails the whole config after jail.local is written). The pattern ends in \Z, not $.
  • meta/argument_specs.yml (typed option; the role description mentions the validation), README, changelog fragment (minor_changes).
  • Documented limits: service_jail does not read the variable; the value replaces an ignoreip set in jail.conf or jail.d/*.conf (read before jail.local); a failed hostname lookup leaves that entry ineffective with a log warning, so addresses and networks are safer; --tags fail2ban_configure skips the validation.
  • Molecule default scenario: converge with a custom list; verify that jail.local has the [DEFAULT] line and that the running daemon ignores the configured network on the sshd jail and on the jail.d service jail svc-a; converge with a whitespace entry, a plain string and a semicolon entry, each wrapped so the play passes, asserting that the failure carries the validation message, and verify none of the rejected values reached jail.local.

Compatibility

The first run after upgrading adds the [DEFAULT] section to jail.local, so Fail2Ban restarts once (the existing Restart handler, which stops the jails and starts them again). Later runs are idempotent.

Verified

  • All pre-commit hooks pass on every commit, and ansible-test sanity passes in the pre-push hook.
  • CI on the current head: Lint, Build and Molecule are green. Molecule selected the fail2ban scenario and passed converge, idempotence and verify, including the new rejection cases and the fail2ban-client get <jail> ignoreip checks.
  • The real validate.yml task run on 16 cases with the role defaults loaded: accepts the default, an extra network, an IPv6 prefix, a hostname, a value containing #, and an empty list; rejects a plain string, a mapping, an integer entry, a null entry, an embedded space, a trailing newline, an empty string, and ; or % in an entry.
  • Two behaviours of Fail2Ban 1.1.0 were checked in its source: inline_comment_prefixes = ";" and BasicInterpolation in the config parser, and the read order jail.conf, jail.d/*.conf, jail.local, jail.d/*.local.

Not verified

  • The empty list (ignoreip = with nothing after it) is rendered as expected but is not covered by Molecule: testing it needs a second jail.local rewrite between converge and verify.
  • The Fail2Ban restart on the first upgrade run of an existing host (Molecule starts from a fresh container).

Assisted-by: Claude Code

Closes #155 ## What `fail2ban_ignoreip`: a list of addresses or networks written as `ignoreip` in a new `[DEFAULT]` section of `jail.local`. The default is Fail2Ban's own (`127.0.0.1/8`, `::1`), so nothing changes unless a consumer sets it. ## Why The role had no way to keep a trusted address from being banned. A mistyped key or a looping health check from the machine that runs Ansible, or from a CI runner, can get that address banned and lock automation out of the host. `[DEFAULT]` covers both the jails of `fail2ban_jails` and the service jails that other roles add with `tasks_from: service_jail` (written to `jail.d`); a per-jail `ignoreip` would need repeating on every jail and would miss service jails. ## Changes - `defaults/main.yml`, `templates/jail.local.j2`: the variable and the `[DEFAULT]` block. - `tasks/validate.yml`: the value must be a list of non-empty strings (`argument_specs` accepts a plain string for a list option and leaves it a string, and accepts numbers and null in the list, so the type is checked here first). Entries must hold no whitespace (they are joined with spaces), no `;` (after whitespace it starts an inline comment in Fail2Ban's config files, which drops the rest of the line without an error) and no `%` (interpolation syntax, which fails the whole config after `jail.local` is written). The pattern ends in `\Z`, not `$`. - `meta/argument_specs.yml` (typed option; the role description mentions the validation), README, changelog fragment (`minor_changes`). - Documented limits: `service_jail` does not read the variable; the value replaces an `ignoreip` set in `jail.conf` or `jail.d/*.conf` (read before `jail.local`); a failed hostname lookup leaves that entry ineffective with a log warning, so addresses and networks are safer; `--tags fail2ban_configure` skips the validation. - Molecule `default` scenario: converge with a custom list; verify that `jail.local` has the `[DEFAULT]` line and that the running daemon ignores the configured network on the `sshd` jail and on the `jail.d` service jail `svc-a`; converge with a whitespace entry, a plain string and a semicolon entry, each wrapped so the play passes, asserting that the failure carries the validation message, and verify none of the rejected values reached `jail.local`. ## Compatibility The first run after upgrading adds the `[DEFAULT]` section to `jail.local`, so Fail2Ban restarts once (the existing `Restart` handler, which stops the jails and starts them again). Later runs are idempotent. ## Verified - All pre-commit hooks pass on every commit, and `ansible-test sanity` passes in the pre-push hook. - CI on the current head: Lint, Build and Molecule are green. Molecule selected the `fail2ban` scenario and passed converge, idempotence and verify, including the new rejection cases and the `fail2ban-client get <jail> ignoreip` checks. - The real `validate.yml` task run on 16 cases with the role defaults loaded: accepts the default, an extra network, an IPv6 prefix, a hostname, a value containing `#`, and an empty list; rejects a plain string, a mapping, an integer entry, a null entry, an embedded space, a trailing newline, an empty string, and `;` or `%` in an entry. - Two behaviours of Fail2Ban 1.1.0 were checked in its source: `inline_comment_prefixes = ";"` and `BasicInterpolation` in the config parser, and the read order `jail.conf`, `jail.d/*.conf`, `jail.local`, `jail.d/*.local`. ## Not verified - The empty list (`ignoreip = ` with nothing after it) is rendered as expected but is not covered by Molecule: testing it needs a second `jail.local` rewrite between converge and verify. - The Fail2Ban restart on the first upgrade run of an existing host (Molecule starts from a fresh container). Assisted-by: Claude Code
Dragonish added 1 commit 2026-11-15 12:02:46 +00:00
feat(fail2ban): add fail2ban_ignoreip to exempt trusted addresses
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 26s
Molecule / molecule (pull_request) Successful in 52s
57b15df1ac
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
Defame1297 added 1 commit 2026-11-15 12:39:52 +00:00
Merge branch 'main' into feat/fail2ban-ignoreip
Build / build (pull_request) Successful in 10s
Lint / lint (pull_request) Successful in 25s
Molecule / molecule (pull_request) Successful in 51s
d77b3778cd
Claude left a comment
Owner

Review of head 57b15df. No blocking issues. The change is mergeable; I'd like the first item below fixed before merge.

What checks out

  • [DEFAULT] in jail.local is the right place: it reaches both the fail2ban_jails jails and the jail.d service jails.
  • The Molecule verify asks the running daemon (fail2ban-client get <jail> ignoreip) for both sshd and svc-a, so it proves the setting takes effect, not just that the file was written.
  • Validation runs before configure, so a rejected value never reaches jail.local. \Z is the correct anchor.
  • Conventions: Conventional Commit, changelog fragment, README, defaults and argument_specs agree. Build, Lint and Molecule are green.

Please fix

  1. A scalar string such as fail2ban_ignoreip: "1.2.3.4" is probably accepted by argument_specs, which splits a str on commas for type: list. The variable then stays a string, select('match') and join(' ') iterate per character, and a plain Jinja render gives ignoreip = 1 . 2 . 3 . 4. I did not run this in Ansible, so please confirm. Suggested fix: add fail2ban_ignoreip is sequence and is not string to the assert in tasks/validate.yml. Then the comment "the type is argument_specs' job" holds.

Nits
2. README: the exemption only exists once the main entry point has written jail.local on that host. service_jail neither reads fail2ban_ignoreip nor runs validate.yml, so a host using only service_jail gets no exemption. Worth one sentence.
3. README: Fail2Ban resolves hostnames when the jail starts, and a DNS failure leaves the entry silently ineffective. Recommend IPs or CIDR over hostnames.
4. The empty-list case (ignoreip = with a trailing space) has no Molecule coverage. Optional.
5. null or non-string entries probably fail with a TypeError in the match filter. That still happens before anything is written, so it only costs a poor error message.
6. Cosmetic: the role description line in argument_specs.yml is much longer than its neighbours, and the PR body has a stray blank line that splits the "Verified" list.

Not verified by me

  • Real Fail2Ban behaviour (I did not run Fail2Ban; precedence and empty-list handling are from its docs).
  • The fail2ban-client get ignoreip output format beyond what the green Molecule run implies.
  • The first-upgrade restart on an existing host, which the PR itself lists as unverified.

🤖 Generated with Claude Code

Review of head 57b15df. No blocking issues. The change is mergeable; I'd like the first item below fixed before merge. **What checks out** - `[DEFAULT]` in `jail.local` is the right place: it reaches both the `fail2ban_jails` jails and the `jail.d` service jails. - The Molecule verify asks the running daemon (`fail2ban-client get <jail> ignoreip`) for both `sshd` and `svc-a`, so it proves the setting takes effect, not just that the file was written. - Validation runs before `configure`, so a rejected value never reaches `jail.local`. `\Z` is the correct anchor. - Conventions: Conventional Commit, changelog fragment, README, defaults and `argument_specs` agree. Build, Lint and Molecule are green. **Please fix** 1. A scalar string such as `fail2ban_ignoreip: "1.2.3.4"` is probably accepted by `argument_specs`, which splits a str on commas for `type: list`. The variable then stays a string, `select('match')` and `join(' ')` iterate per character, and a plain Jinja render gives `ignoreip = 1 . 2 . 3 . 4`. I did not run this in Ansible, so please confirm. Suggested fix: add `fail2ban_ignoreip is sequence and is not string` to the assert in `tasks/validate.yml`. Then the comment "the type is argument_specs' job" holds. **Nits** 2. README: the exemption only exists once the main entry point has written `jail.local` on that host. `service_jail` neither reads `fail2ban_ignoreip` nor runs `validate.yml`, so a host using only `service_jail` gets no exemption. Worth one sentence. 3. README: Fail2Ban resolves hostnames when the jail starts, and a DNS failure leaves the entry silently ineffective. Recommend IPs or CIDR over hostnames. 4. The empty-list case (`ignoreip = ` with a trailing space) has no Molecule coverage. Optional. 5. `null` or non-string entries probably fail with a `TypeError` in the `match` filter. That still happens before anything is written, so it only costs a poor error message. 6. Cosmetic: the role description line in `argument_specs.yml` is much longer than its neighbours, and the PR body has a stray blank line that splits the "Verified" list. **Not verified by me** - Real Fail2Ban behaviour (I did not run Fail2Ban; precedence and empty-list handling are from its docs). - The `fail2ban-client get ignoreip` output format beyond what the green Molecule run implies. - The first-upgrade restart on an existing host, which the PR itself lists as unverified. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Defame1297 added 1 commit 2026-11-15 13:26:56 +00:00
Merge branch 'main' into feat/fail2ban-ignoreip
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 26s
Molecule / molecule (pull_request) Successful in 51s
9f0e0bda85
Dragonish added 3 commits 2026-11-15 14:10:35 +00:00
argument_specs accepts a plain string for a list option and leaves it a string,
and accepts a list holding a number or null. The pattern check then iterated a
string per character and rendered "1 . 2 . 3 . 4", or died with a TypeError on
null. Check the type first (assert stops at the first false item).

Also refuse ";" and "%" in an entry: Fail2Ban's config parser treats ";" after
whitespace as an inline comment, which drops the rest of the ignoreip line
without an error, and "%" as interpolation syntax, which fails the whole config
after jail.local is written. Add Molecule cases for a scalar string and a
semicolon entry.

Document what review found: service_jail does not read the variable, the
[DEFAULT] value replaces an ignoreip set in jail.conf or jail.d/*.conf, a failed
hostname lookup leaves the entry ineffective with a log warning (addresses and
networks are safer), and --tags fail2ban_configure skips the validation.

Assisted-by: Claude Code
test(fail2ban): match the validation message in the ignoreip rejection cases
Build / build (pull_request) Successful in 13s
Lint / lint (pull_request) Successful in 28s
Molecule / molecule (pull_request) Successful in 1m2s
9dcb933bf9
The three rejection cases only printed "Rejected as expected", so they would
also pass when the role failed for another reason. Assert that the failure
carries the validation message of fail2ban_ignoreip.

Assisted-by: Claude Code
Author
Collaborator

Thanks for the review. Pushed 9dcb933 (fast-forward; your two merges of main are included). Lint, Build and Molecule are green on it.

Fixed

  1. Confirmed in Ansible before changing anything: argument_specs accepts "1.2.3.4" for the list option and the variable stays a string, so the old assert passed and the template rendered 1 . 2 . 3 . 4. validate.yml now checks that the value is a list, with no mapping and no non-string entries, before the pattern check (an assert stops at the first false item, so the later items never see a value they cannot handle).
  2. Numbers and null in the list now fail with the validation message instead of a TypeError. That is the same change.
  3. README: service_jail does not read fail2ban_ignoreip, so a host that uses only service_jail has no exemption.
  4. README and argument_specs: prefer addresses and networks to hostnames. I checked the source: a failed lookup logs a warning (Unable to find a corresponding IP address for ...), so I did not call it silent.
  5. The long description line is wrapped and the PR body no longer splits the Verified list.

Also found while checking your points (Fail2Ban 1.1.0 source)

  • The config parser sets inline_comment_prefixes = ";", so an entry starting with ; would drop the rest of the ignoreip line without an error, and -t cannot see it. % is interpolation syntax and fails the whole config after jail.local is written. Both are now refused. I left # allowed: it is not an inline comment prefix there.
  • jail.d/*.conf is read before jail.local, so the new [DEFAULT] value replaces an ignoreip set there. That is documented now, as is the fact that --tags fail2ban_configure skips the validation.

Tests: new rejection cases for a plain string and a ; entry. All three rejection cases now assert that the failure carries the validation message, so they cannot pass for another reason.

Not done (4): the empty-list case has no Molecule test. It would need a second jail.local rewrite between converge and verify. I checked the rendering locally (ignoreip = ) and listed it under "Not verified" in the body.

Assisted-by: Claude Code

Thanks for the review. Pushed `9dcb933` (fast-forward; your two merges of `main` are included). Lint, Build and Molecule are green on it. **Fixed** 1. Confirmed in Ansible before changing anything: `argument_specs` accepts `"1.2.3.4"` for the list option and the variable stays a string, so the old assert passed and the template rendered `1 . 2 . 3 . 4`. `validate.yml` now checks that the value is a list, with no mapping and no non-string entries, before the pattern check (an `assert` stops at the first false item, so the later items never see a value they cannot handle). 5. Numbers and `null` in the list now fail with the validation message instead of a `TypeError`. That is the same change. 2. README: `service_jail` does not read `fail2ban_ignoreip`, so a host that uses only `service_jail` has no exemption. 3. README and `argument_specs`: prefer addresses and networks to hostnames. I checked the source: a failed lookup logs a warning (`Unable to find a corresponding IP address for ...`), so I did not call it silent. 6. The long description line is wrapped and the PR body no longer splits the Verified list. **Also found while checking your points (Fail2Ban 1.1.0 source)** - The config parser sets `inline_comment_prefixes = ";"`, so an entry starting with `;` would drop the rest of the `ignoreip` line without an error, and `-t` cannot see it. `%` is interpolation syntax and fails the whole config after `jail.local` is written. Both are now refused. I left `#` allowed: it is not an inline comment prefix there. - `jail.d/*.conf` is read before `jail.local`, so the new `[DEFAULT]` value replaces an `ignoreip` set there. That is documented now, as is the fact that `--tags fail2ban_configure` skips the validation. **Tests:** new rejection cases for a plain string and a `;` entry. All three rejection cases now assert that the failure carries the validation message, so they cannot pass for another reason. **Not done (4):** the empty-list case has no Molecule test. It would need a second `jail.local` rewrite between converge and verify. I checked the rendering locally (`ignoreip = `) and listed it under "Not verified" in the body. Assisted-by: Claude Code
Defame1297 merged commit faf449c9b2 into main 2026-11-15 17:31:40 +00:00
Defame1297 deleted branch feat/fail2ban-ignoreip 2026-11-15 17:31:40 +00:00
Sign in to join this conversation.
No Reviewers
3 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#156