Files
Kamino/roles/ufw/tasks/enable.yml
T
Defame1297 924a0ea7a7 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
2026-11-29 16:39:45 +00:00

35 lines
1.0 KiB
YAML

---
# community.general.ufw runs `ufw -f enable` and `ufw logging` unconditionally
# (rewriting /etc/ufw/user.rules and re-applying sysctl.conf), so only call it
# when UFW is inactive or its logging setting differs from ufw_logging.
- name: Read current UFW status
become: true
ansible.builtin.command: ufw status verbose
register: ufw_status_verbose
changed_when: false
check_mode: false
environment:
LC_ALL: C
LANGUAGE: C
when:
- ufw_enabled
- __ufw_can_configure
- name: Enable UFW
become: true
community.general.ufw:
state: enabled
logging: "{{ ufw_logging }}"
vars:
# "on" matches any level ("Logging: on (low)"); "off" and levels match exactly.
ufw_logging_expected: >-
{{ 'Logging: off' if ufw_logging == 'off'
else 'Logging: on' if ufw_logging == 'on'
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