diff --git a/changelogs/fragments/505-ufw-check-mode-probe.yml b/changelogs/fragments/505-ufw-check-mode-probe.yml new file mode 100644 index 0000000..3031fb9 --- /dev/null +++ b/changelogs/fragments/505-ufw-check-mode-probe.yml @@ -0,0 +1,5 @@ +--- +trivial: + - >- + ufw - The check-mode probes read whether a command ran instead of whether it was skipped, which + ansible-core 2.25 stops reporting for a command in check mode. diff --git a/roles/ufw/README.md b/roles/ufw/README.md index 1fd6bc1..6b8ccd2 100644 --- a/roles/ufw/README.md +++ b/roles/ufw/README.md @@ -45,7 +45,7 @@ There is no discovery by variable name, and a variable that looks like one fails ### `service_rules` -A service role that owns firewall rules calls the role with `include_role` and `tasks_from: service_rules` after the main entry has run, with `apply: {tags: [...]}` so `--tags ` reaches these tasks. The entry point installs nothing and touches no default policy, no `before.rules` and no base rule. Outside check mode (`--check` or the `check_mode` keyword; the role detects it with a skipped probe task, not `ansible_check_mode`) it fails when ufw is not installed and active. In check mode it skips the reconcile only when `ufw status` fails (ufw not installed yet, the main entry may not have reached the host); a host with ufw installed but inactive is reconciled as usual. +A service role that owns firewall rules calls the role with `include_role` and `tasks_from: service_rules` after the main entry has run, with `apply: {tags: [...]}` so `--tags ` reaches these tasks. The entry point installs nothing and touches no default policy, no `before.rules` and no base rule. Outside check mode (`--check` or the `check_mode` keyword; the role detects it with a probe command that does not run there, not `ansible_check_mode`) it fails when ufw is not installed and active. In check mode it skips the reconcile only when `ufw status` fails (ufw not installed yet, the main entry may not have reached the host); a host with ufw installed but inactive is reconciled as usual. | Variable | Default | Required | Description | |---|---|---|---| diff --git a/roles/ufw/molecule/default/converge.yml b/roles/ufw/molecule/default/converge.yml index 9407664..eb80cee 100644 --- a/roles/ufw/molecule/default/converge.yml +++ b/roles/ufw/molecule/default/converge.yml @@ -68,7 +68,7 @@ # svc_o2 that looks like the SSH rule, and a rule of svc_m. The managed owners below are # the ones used above plus svc_m (listed but never called), so svc_o1 and svc_o2 are # orphans. The cases that must change nothing run first and the purge last, so the -# idempotence run finds nothing left to delete. Under --check the role's own skipped probe +# idempotence run finds nothing left to delete. Under --check the role's own check-mode probe # keeps the purge from deleting, and orphan_check.yml skips its counts. - name: Report and purge the rules of unmanaged owners hosts: all diff --git a/roles/ufw/molecule/default/verify.yml b/roles/ufw/molecule/default/verify.yml index 0adc3c1..f30fcce 100644 --- a/roles/ufw/molecule/default/verify.yml +++ b/roles/ufw/molecule/default/verify.yml @@ -474,8 +474,8 @@ # apply of community.general.ufw reports changed for the would-be rule, so molecule's # idempotence run (a second converge) would fail on it. Verify runs once, outside that count. # The play runs under the check_mode keyword, which does not set ansible_check_mode in the -# included role; the role detects check mode with a probe task instead (a command task is -# skipped under the keyword). A present call that would add a rule and an absent call for an +# included role; the role detects check mode with a probe task instead (a command does +# not run under the keyword). A present call that would add a rule and an absent call for an # owner that holds rules must change nothing. The two reads of the rule list are forced to # run (check_mode: false) and compared as a whole. - name: Check mode of service_rules changes nothing diff --git a/roles/ufw/tasks/install.yml b/roles/ufw/tasks/install.yml index bb0d2a3..1177dbe 100644 --- a/roles/ufw/tasks/install.yml +++ b/roles/ufw/tasks/install.yml @@ -7,9 +7,10 @@ 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. +# include_role tasks. A command does not run in either form, so its output is missing; the same +# probe as tasks/sysctl.yml. Not `is skipped`: ansible-core 2.25 drops that check-mode result. - name: Probe whether the run is in check mode - ansible.builtin.command: "true" + ansible.builtin.command: echo ran register: __ufw_install_probe changed_when: false @@ -17,4 +18,4 @@ # 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 }}" + __ufw_can_configure: "{{ __ufw_install is not changed or __ufw_install_probe.stdout == 'ran' }}" diff --git a/roles/ufw/tasks/orphan_rules.yml b/roles/ufw/tasks/orphan_rules.yml index c5e7ea8..c0a411e 100644 --- a/roles/ufw/tasks/orphan_rules.yml +++ b/roles/ufw/tasks/orphan_rules.yml @@ -64,16 +64,16 @@ quiet: true # ansible_check_mode is not used: the play-level `check_mode: true` keyword does not set - # that magic variable inside include_role tasks (only --check does). A command task is - # skipped in both forms and its result says so, which is the one signal that holds for both. + # that magic variable inside include_role tasks (only --check does). A command does not run in + # either form, so its output is missing. Not `is skipped`: ansible-core 2.25 drops that result. - name: Probe whether the run is in check mode - ansible.builtin.command: "true" + ansible.builtin.command: echo ran register: ufw_oa_probe changed_when: false - name: Remember whether the run is in check mode ansible.builtin.set_fact: - ufw_oa_check: "{{ ufw_oa_probe is skipped }}" + ufw_oa_check: "{{ ufw_oa_probe.stdout != 'ran' }}" - name: Read the firewall status become: true diff --git a/roles/ufw/tasks/service_rules.yml b/roles/ufw/tasks/service_rules.yml index b18578b..d988d7b 100644 --- a/roles/ufw/tasks/service_rules.yml +++ b/roles/ufw/tasks/service_rules.yml @@ -122,16 +122,16 @@ tags: [ufw, ufw_service_rules] block: # ansible_check_mode is not used: the play-level `check_mode: true` keyword does not set - # that magic variable inside include_role tasks (only --check does). A command task is - # skipped in both forms and its result says so, which is the one signal that holds for both. + # that magic variable inside include_role tasks (only --check does). A command does not run in + # either form, so its output is missing. Not `is skipped`: ansible-core 2.25 drops that result. - name: Probe whether the run is in check mode - ansible.builtin.command: "true" + ansible.builtin.command: echo ran register: ufw_sr_probe changed_when: false - name: Remember whether the run is in check mode ansible.builtin.set_fact: - ufw_sr_check: "{{ ufw_sr_probe is skipped }}" + ufw_sr_check: "{{ ufw_sr_probe.stdout != 'ran' }}" - name: Read the firewall status become: true diff --git a/roles/ufw/tasks/sysctl.yml b/roles/ufw/tasks/sysctl.yml index bddbdc5..b47ffb7 100644 --- a/roles/ufw/tasks/sysctl.yml +++ b/roles/ufw/tasks/sysctl.yml @@ -8,17 +8,18 @@ when: ufw_enabled and ufw_sysctl_overrides | length > 0 # ansible_check_mode is not used: the play-level `check_mode: true` keyword does not set that -# magic variable inside include_role tasks (only --check does). A command task -# is skipped in both forms and its result says so, the same probe as tasks/service_rules.yml. +# magic variable inside include_role tasks (only --check does). A command does not run in either +# form, so its output is missing; the same probe as tasks/service_rules.yml. Not `is skipped`: +# ansible-core 2.25 drops that check-mode result. - name: Probe whether the run is in check mode - ansible.builtin.command: "true" + ansible.builtin.command: echo ran register: ufw_sysctl_probe changed_when: false when: ufw_enabled and ufw_sysctl_overrides | length > 0 - name: Remember whether the run is in check mode ansible.builtin.set_fact: - ufw_sysctl_check: "{{ ufw_sysctl_probe is skipped }}" + ufw_sysctl_check: "{{ ufw_sysctl_probe.stdout != 'ran' }}" when: ufw_enabled and ufw_sysctl_overrides | length > 0 # In check mode on a fresh host ufw is not installed yet, so the file is absent.