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

+3 -1
View File
@@ -8,7 +8,9 @@
group: root
mode: "0644"
notify: Reload UFW
when: ufw_enabled
when:
- ufw_enabled
- __ufw_can_configure
# Not gated on ufw_aggregated_before_rules being non-empty: this template
# fully replaces the stock before.rules (unlike rules.yml, which only adds
# to it), and its unconditional loopback-ACCEPT block must always be
+9 -3
View File
@@ -4,18 +4,24 @@
community.general.ufw:
direction: incoming
policy: "{{ ufw_defaults.incoming }}"
when: ufw_enabled
when:
- ufw_enabled
- __ufw_can_configure
- name: Set default outgoing policy
become: true
community.general.ufw:
direction: outgoing
policy: "{{ ufw_defaults.outgoing }}"
when: ufw_enabled
when:
- ufw_enabled
- __ufw_can_configure
- name: Set default routed policy
become: true
community.general.ufw:
direction: routed
policy: "{{ ufw_defaults.routed }}"
when: ufw_enabled
when:
- ufw_enabled
- __ufw_can_configure
+4 -1
View File
@@ -11,7 +11,9 @@
environment:
LC_ALL: C
LANGUAGE: C
when: ufw_enabled
when:
- ufw_enabled
- __ufw_can_configure
- name: Enable UFW
become: true
@@ -26,6 +28,7 @@
else 'Logging: on (' ~ ufw_logging ~ ')' }}
when:
- ufw_enabled
- __ufw_can_configure
- >-
'Status: active' not in ufw_status_verbose.stdout
or ufw_logging_expected not in ufw_status_verbose.stdout
+8
View File
@@ -4,3 +4,11 @@
ansible.builtin.package:
name: "{{ ufw_package_name }}"
state: present
register: __ufw_install
# ansible_check_mode is not used: the play-level `check_mode: true` keyword does not set it inside
# include_role tasks. A command task is skipped in both forms, the same probe as tasks/sysctl.yml.
- name: Probe whether the run is in check mode
ansible.builtin.command: "true"
register: __ufw_install_probe
changed_when: false
+1
View File
@@ -23,3 +23,4 @@
when:
- ufw_enabled
- ufw_manage_rules
- __ufw_can_configure