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

Merged
Defame1297 merged 3 commits from fix/478-ufw-fail2ban-check-mode into main 2026-11-29 18:08:28 +00:00
Owner

What and why

Closes #478

In check mode the package install reports changed, but nothing is installed, so later tasks that need the binary or its config failed on a fresh host.

fail2ban

  • The install is registered as __fail2ban_install.
  • These are skipped with not ansible_check_mode or __fail2ban_install | default({}) is not changed:
    • the jail.local, filter and jail templates;
    • both fail2ban-client -t tests;
    • the systemd task;
    • the jail-status loop;
    • the Reload handler.
  • flush_handlers stays unguarded, because nothing is notified once those tasks skip.

ufw

  • The install is registered as __ufw_install. A command: "true" check-mode probe follows it, the same pattern as sysctl.yml, because ansible_check_mode is not set for a play-level check_mode inside include_role.
  • A set_fact right after the probe gives __ufw_can_configure. It guards before.rules, the default policies, the rules and enable. Guards read it with default(true), so a run that skips the install by tags behaves as before.
  • The service_rules entry point already passes, because its ufw status has failed_when: false and its assert skips in check mode. The new scenario exercises it.

Docs, changelog and tests

  • Both READMEs drop the "--check fails without the package" limit. They keep "play-level check_mode: true is not supported" and say a preview on a fresh host skips configuration.
  • A bugfix changelog fragment is added.
  • There are two new scenarios, roles/ufw/molecule/check-absent and roles/fail2ban/molecule/check-absent, each running dependency, create, prepare and check. Prepare purges the package and refreshes APT. There is one per role, because a shared scenario would make the selector count ufw as a consumer of fail2ban.

This unblocks #435, which stops baking ufw and fail2ban into the Molecule images.

Verified

  • pre-commit run --all-files passes, ansible-lint included.
  • python -m unittest discover -s tests/scripts passes.
  • ansible-playbook --syntax-check passes on the new converge and prepare playbooks.
  • The ufw check-absent converge run locally with --check on a host without ufw gave failed=0.
  • Two clean-context review rounds traced every task of both roles. The first found no blocking issues; I applied three of its notes:
    • the computed fact moved out of vars/;
    • service_rules was checked;
    • a misplaced comment was moved.
  • CI: this pull request's Molecule run covers the new scenarios and the existing ones, idempotence included.

Not verified

  • The new scenarios, and the default scenarios' idempotence, have run only in CI.
  • The probe depends on the command module reporting skipped in check mode. ansible-core plans to change that in 2.25. sysctl.yml already relies on it.

  • I confirm third-party material is recorded in NOTICE and REUSE.toml, or there is none.

Assisted-by: Claude Code

## What and why Closes #478 In check mode the package install reports changed, but nothing is installed, so later tasks that need the binary or its config failed on a fresh host. **fail2ban** - The install is registered as `__fail2ban_install`. - These are skipped with `not ansible_check_mode or __fail2ban_install | default({}) is not changed`: - the `jail.local`, filter and jail templates; - both `fail2ban-client -t` tests; - the systemd task; - the jail-status loop; - the Reload handler. - `flush_handlers` stays unguarded, because nothing is notified once those tasks skip. **ufw** - The install is registered as `__ufw_install`. A `command: "true"` check-mode probe follows it, the same pattern as `sysctl.yml`, because `ansible_check_mode` is not set for a play-level `check_mode` inside `include_role`. - A `set_fact` right after the probe gives `__ufw_can_configure`. It guards `before.rules`, the default policies, the rules and enable. Guards read it with `default(true)`, so a run that skips the install by tags behaves as before. - The `service_rules` entry point already passes, because its `ufw status` has `failed_when: false` and its assert skips in check mode. The new scenario exercises it. **Docs, changelog and tests** - Both READMEs drop the "--check fails without the package" limit. They keep "play-level `check_mode: true` is not supported" and say a preview on a fresh host skips configuration. - A bugfix changelog fragment is added. - There are two new scenarios, `roles/ufw/molecule/check-absent` and `roles/fail2ban/molecule/check-absent`, each running dependency, create, prepare and check. Prepare purges the package and refreshes APT. There is one per role, because a shared scenario would make the selector count ufw as a consumer of fail2ban. This unblocks #435, which stops baking ufw and fail2ban into the Molecule images. ## Verified - `pre-commit run --all-files` passes, ansible-lint included. - `python -m unittest discover -s tests/scripts` passes. - `ansible-playbook --syntax-check` passes on the new converge and prepare playbooks. - The ufw check-absent converge run locally with `--check` on a host without ufw gave `failed=0`. - Two clean-context review rounds traced every task of both roles. The first found no blocking issues; I applied three of its notes: - the computed fact moved out of `vars/`; - `service_rules` was checked; - a misplaced comment was moved. - CI: this pull request's Molecule run covers the new scenarios and the existing ones, idempotence included. ## Not verified - The new scenarios, and the default scenarios' idempotence, have run only in CI. - The probe depends on the command module reporting `skipped` in check mode. ansible-core plans to change that in 2.25. `sysctl.yml` already relies on it. --- - [x] I confirm third-party material is recorded in `NOTICE` and `REUSE.toml`, or there is none. Assisted-by: Claude Code
Claude added this to the CI velocity (emergency) milestone 2026-11-29 17:44:06 +00:00
Claude added 2 commits 2026-11-29 17:44:06 +00:00
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
fix(ufw, fail2ban): keep the check-mode guard out of vars and test service_rules
Build / build (pull_request) Successful in 38s
Lint / lint (pull_request) Successful in 4m29s
Molecule / molecule (pull_request) Canceled after 1m14s
a9a973a523
vars/ holds constants only, so ufw's __ufw_can_configure is now a
set_fact after the install probe; the guards default it to true when the
install tasks were skipped by tags.

service_rules already skips its reconcile when `ufw status` fails under
check mode; the ufw check-absent scenario now calls it so CI holds that.
The fail2ban service jail task's tags sit with the task again, above the
comment of the configuration test.

Refs #478

Assisted-by: Claude Code
Defame1297 added 1 commit 2026-11-29 17:54:16 +00:00
Merge branch 'main' into fix/478-ufw-fail2ban-check-mode
Build / build (pull_request) Successful in 40s
Lint / lint (pull_request) Successful in 3m45s
Molecule / molecule (pull_request) Successful in 13m26s
c402720376
Defame1297 approved these changes 2026-11-29 18:00:03 +00:00
Defame1297 scheduled this pull request to auto merge when all checks succeed 2026-11-29 18:00:13 +00:00
Defame1297 merged commit 7f28fccfe4 into main 2026-11-29 18:08:28 +00:00
Defame1297 deleted branch fix/478-ufw-fail2ban-check-mode 2026-11-29 18:08:28 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#481