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

@@ -0,0 +1,21 @@
---
# Run only under --check (the check step): fail2ban is not installed, so the tasks that need it skip.
- name: Converge the base role on a host without fail2ban
hosts: all
roles:
- role: mortis.kamino.fail2ban
- name: Converge a service jail on a host without fail2ban
hosts: all
tasks:
- name: Service jail with a journalmatch
ansible.builtin.include_role:
name: mortis.kamino.fail2ban
tasks_from: service_jail
vars:
fail2ban_service_name: svc-a
fail2ban_service_jail:
port: "8080"
journalmatch: "_SYSTEMD_UNIT=ssh.service"
fail2ban_service_filter:
failregex: "^.*denied for <HOST>.*$"
@@ -0,0 +1,55 @@
---
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404
# (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in
# .gitea/workflows/molecule.yml. No privileged mode: systemd needs a writable cgroup and the
# firewall tools need NET_ADMIN, nothing else. Needs the network:
# prepare purges the fail2ban the image ships and refreshes the APT cache the image deletes.
driver:
name: docker
platforms:
- name: ${KAMINO_RUN_ID:-local}-fail2ban-check-absent-instance
labels:
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
image: ${MOLECULE_IMAGE}
pre_build_image: true
pull: true
command: /sbin/init
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /run
- /run/lock
capabilities:
- NET_ADMIN
security_opts:
- apparmor=unconfined
- name: ${KAMINO_RUN_ID:-local}-fail2ban-check-absent-2604-instance
labels:
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
image: ${MOLECULE_IMAGE_2604}
pre_build_image: true
pull: true
command: /sbin/init
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /run
- /run/lock
capabilities:
- NET_ADMIN
security_opts:
- apparmor=unconfined
provisioner:
name: ansible
verifier:
name: ansible
scenario:
# Previews the role on a host without fail2ban (--check only, nothing is applied); no destroy:
# scripts/run_molecule_scenarios.sh removes the container as soon as this scenario ends.
test_sequence:
- dependency
- create
- prepare
- check
@@ -0,0 +1,16 @@
---
# The image ships fail2ban and no APT lists; the check step needs fail2ban gone and the lists back,
# or the install task cannot even preview the package.
- name: Prepare a host without fail2ban
hosts: all
gather_facts: false
tasks:
- name: Purge fail2ban
ansible.builtin.apt:
name: fail2ban
state: absent
purge: true
- name: Refresh the APT cache
ansible.builtin.apt:
update_cache: true