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
42 lines
1.5 KiB
YAML
42 lines
1.5 KiB
YAML
---
|
|
# -t loads every jail, so a jail that cannot start (missing logpath, bad
|
|
# backend) fails here. -d only dumps the config and treats those as warnings.
|
|
- name: Test Fail2Ban configuration
|
|
become: true
|
|
ansible.builtin.command: fail2ban-client -t
|
|
register: fail2ban_test
|
|
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
|
|
ansible.builtin.systemd:
|
|
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.
|
|
- name: Apply pending Fail2Ban handlers
|
|
ansible.builtin.meta: flush_handlers
|
|
|
|
- name: Verify enabled jails are loaded
|
|
become: true
|
|
ansible.builtin.command: "fail2ban-client status {{ item.name }}"
|
|
loop: "{{ fail2ban_jails | selectattr('enabled') }}"
|
|
loop_control:
|
|
label: "{{ item.name }}"
|
|
register: fail2ban_jail_status
|
|
until: fail2ban_jail_status.rc == 0
|
|
retries: 5
|
|
delay: 2
|
|
changed_when: false
|
|
check_mode: false
|
|
when:
|
|
- not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
|
- fail2ban_service.state | default('started') == 'started'
|