fix(ufw, fail2ban): keep the check-mode guard out of vars and test service_rules
vars/ holds constants only, so ufw's __ufw_can_configure is now a set_fact after the install probe; the guards default it to true when the install tasks were skipped by tags. service_rules already skips its reconcile when `ufw status` fails under check mode; the ufw check-absent scenario now calls it so CI holds that. The fail2ban service jail task's tags sit with the task again, above the comment of the configuration test. Refs #478 Assisted-by: Claude Code
This commit is contained in:
1 parent
924a0ea7a7
commit
a9a973a523
9 files changed
+29
-13
No files matched your search
@@ -47,11 +47,10 @@
|
|||||||
mode: "0644"
|
mode: "0644"
|
||||||
notify: Reload Fail2Ban
|
notify: Reload Fail2Ban
|
||||||
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
when: not ansible_check_mode or __fail2ban_install | default({}) is not changed
|
||||||
|
tags: [fail2ban, fail2ban_service_jail]
|
||||||
|
|
||||||
# Read-only, so it also runs in check mode. A failure leaves the broken file on
|
# 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.
|
# disk and fails every run until fixed, instead of only the run that wrote it.
|
||||||
tags: [fail2ban, fail2ban_service_jail]
|
|
||||||
|
|
||||||
- name: Test Fail2Ban configuration
|
- name: Test Fail2Ban configuration
|
||||||
become: true
|
become: true
|
||||||
ansible.builtin.command: fail2ban-client -t
|
ansible.builtin.command: fail2ban-client -t
|
||||||
|
|||||||
+1
-1
@@ -145,7 +145,7 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_
|
|||||||
- Verify checks `before.rules`: the loopback accept, the INVALID-state drop behind its logging rule and in its place among the input rules.
|
- Verify checks `before.rules`: the loopback accept, the INVALID-state drop behind its logging rule and in its place among the input rules.
|
||||||
- Verify runs `service_rules` under the `check_mode` keyword and checks that the rule list did not change.
|
- Verify runs `service_rules` under the `check_mode` keyword and checks that the rule list did not change.
|
||||||
|
|
||||||
The scenario `molecule/check-absent/` runs on the same two platforms: prepare purges ufw and refreshes the APT cache, then `check` runs the main entry point under `--check`, which must pass with ufw not installed. Nothing is applied.
|
The scenario `molecule/check-absent/` runs on the same two platforms: prepare purges ufw and refreshes the APT cache, then `check` runs the main entry point and `service_rules` under `--check`, which must pass with ufw not installed. Nothing is applied.
|
||||||
|
|
||||||
Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set, and `orphan_rules` runs in check mode only under `--check` (the `check` steps), not under the `check_mode` keyword.
|
Not covered: `ufw_before_rules` entries and `ufw_sysctl_overrides` are never set, and `orphan_rules` runs in check mode only under `--check` (the `check` steps), not under the `check_mode` keyword.
|
||||||
|
|
||||||
|
|||||||
@@ -10,3 +10,17 @@
|
|||||||
ufw_rules:
|
ufw_rules:
|
||||||
- {rule: allow, port: 22, proto: tcp, comment: ssh}
|
- {rule: allow, port: 22, proto: tcp, comment: ssh}
|
||||||
- {rule: allow, port: 8080, proto: tcp, comment: web}
|
- {rule: allow, port: 8080, proto: tcp, comment: web}
|
||||||
|
|
||||||
|
# The entry point a service role calls: its `ufw status` read fails on this host, so it must skip.
|
||||||
|
- name: Converge the rules of a service on a host without ufw
|
||||||
|
hosts: all
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Rules of one service through the entry point
|
||||||
|
ansible.builtin.include_role:
|
||||||
|
name: mortis.kamino.ufw
|
||||||
|
tasks_from: service_rules
|
||||||
|
vars:
|
||||||
|
ufw_service_name: svc_a
|
||||||
|
ufw_service_rules:
|
||||||
|
- {rule: allow, port: 8001, proto: tcp, comment: alpha web}
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
notify: Reload UFW
|
notify: Reload UFW
|
||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
# Not gated on ufw_aggregated_before_rules being non-empty: this template
|
# Not gated on ufw_aggregated_before_rules being non-empty: this template
|
||||||
# fully replaces the stock before.rules (unlike rules.yml, which only adds
|
# fully replaces the stock before.rules (unlike rules.yml, which only adds
|
||||||
# to it), and its unconditional loopback-ACCEPT block must always be
|
# to it), and its unconditional loopback-ACCEPT block must always be
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
policy: "{{ ufw_defaults.incoming }}"
|
policy: "{{ ufw_defaults.incoming }}"
|
||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
|
|
||||||
- name: Set default outgoing policy
|
- name: Set default outgoing policy
|
||||||
become: true
|
become: true
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
policy: "{{ ufw_defaults.outgoing }}"
|
policy: "{{ ufw_defaults.outgoing }}"
|
||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
|
|
||||||
- name: Set default routed policy
|
- name: Set default routed policy
|
||||||
become: true
|
become: true
|
||||||
@@ -24,4 +24,4 @@
|
|||||||
policy: "{{ ufw_defaults.routed }}"
|
policy: "{{ ufw_defaults.routed }}"
|
||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
LANGUAGE: C
|
LANGUAGE: C
|
||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
|
|
||||||
- name: Enable UFW
|
- name: Enable UFW
|
||||||
become: true
|
become: true
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
else 'Logging: on (' ~ ufw_logging ~ ')' }}
|
else 'Logging: on (' ~ ufw_logging ~ ')' }}
|
||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
- >-
|
- >-
|
||||||
'Status: active' not in ufw_status_verbose.stdout
|
'Status: active' not in ufw_status_verbose.stdout
|
||||||
or ufw_logging_expected not in ufw_status_verbose.stdout
|
or ufw_logging_expected not in ufw_status_verbose.stdout
|
||||||
@@ -12,3 +12,9 @@
|
|||||||
ansible.builtin.command: "true"
|
ansible.builtin.command: "true"
|
||||||
register: __ufw_install_probe
|
register: __ufw_install_probe
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
|
||||||
|
# False only when a check-mode run would install ufw: the binary and /etc/ufw are then missing.
|
||||||
|
# Undefined when this file's tags are skipped; the guards then default it to true.
|
||||||
|
- name: Remember whether ufw can be configured
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
__ufw_can_configure: "{{ __ufw_install is not changed or __ufw_install_probe is not skipped }}"
|
||||||
@@ -23,4 +23,4 @@
|
|||||||
when:
|
when:
|
||||||
- ufw_enabled
|
- ufw_enabled
|
||||||
- ufw_manage_rules
|
- ufw_manage_rules
|
||||||
- __ufw_can_configure
|
- __ufw_can_configure | default(true)
|
||||||
@@ -1,5 +1,2 @@
|
|||||||
---
|
---
|
||||||
ufw_package_name: ufw
|
ufw_package_name: ufw
|
||||||
# False only when a check-mode run would install ufw: the binary and /etc/ufw are then missing, so
|
|
||||||
# the tasks that need them are skipped. True when install.yml did not run (its tags were skipped).
|
|
||||||
__ufw_can_configure: "{{ __ufw_install | default({}) is not changed or __ufw_install_probe is not skipped }}"
|
|
||||||
Reference in new issue
Block a user