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:
1 parent
0173f6f226
commit
924a0ea7a7
20 files changed
+234
-9
No files matched your search
@@ -8,3 +8,5 @@
|
||||
group: root
|
||||
mode: "0600"
|
||||
notify: Reload Fail2Ban
|
||||
# Under --check on a fresh host the package is not installed, so /etc/fail2ban is missing.
|
||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
@@ -7,3 +7,4 @@
|
||||
# Required for `backend = systemd` jails (journalmatch), e.g. gitea's.
|
||||
- python3-systemd
|
||||
state: present
|
||||
register: __fail2ban_install
|
||||
@@ -8,6 +8,8 @@
|
||||
failed_when: fail2ban_test.rc != 0
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
# Under --check on a fresh host the package is not installed: no fail2ban-client and no unit.
|
||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
|
||||
- name: Ensure Fail2Ban service is running
|
||||
become: true
|
||||
@@ -15,6 +17,7 @@
|
||||
name: fail2ban
|
||||
enabled: "{{ fail2ban_service.enabled | default(true) }}"
|
||||
state: "{{ fail2ban_service.state | default('started') }}"
|
||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
|
||||
# Handlers run at the end of the play; flush so the reload happens after the
|
||||
# configuration test above and before the jail check below.
|
||||
@@ -33,4 +36,6 @@
|
||||
delay: 2
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
when: fail2ban_service.state | default('started') == 'started'
|
||||
when:
|
||||
- not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
- fail2ban_service.state | default('started') == 'started'
|
||||
@@ -33,6 +33,8 @@
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: Reload Fail2Ban
|
||||
# Under --check on a fresh host the package is not installed, so /etc/fail2ban is missing.
|
||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
tags: [fail2ban, fail2ban_service_jail]
|
||||
|
||||
- name: Deploy service jail
|
||||
@@ -44,6 +46,7 @@
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: Reload Fail2Ban
|
||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
|
||||
# Read-only, so it also runs in check mode. A failure leaves the broken file on
|
||||
# disk and fails every run until fixed, instead of only the run that wrote it.
|
||||
@@ -54,4 +57,5 @@
|
||||
ansible.builtin.command: fail2ban-client -t
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||
tags: [fail2ban, fail2ban_service_jail]
|
||||
Reference in new issue
Block a user