--- - name: Check whether ufw sysctl.conf exists become: true ansible.builtin.stat: path: /etc/ufw/sysctl.conf register: ufw_sysctl_stat check_mode: false 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. - name: Probe whether the run is in check mode ansible.builtin.command: "true" 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 }}" 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. - name: Set sysctl overrides in ufw sysctl.conf become: true ansible.builtin.lineinfile: path: /etc/ufw/sysctl.conf regexp: '^#?\s*{{ ufw_sysctl_override.name }}=' line: "{{ ufw_sysctl_override.name }}={{ ufw_sysctl_override.value }}" owner: root group: root mode: "0644" loop: "{{ ufw_sysctl_overrides }}" loop_control: loop_var: ufw_sysctl_override when: ufw_enabled and (ufw_sysctl_stat.stat.exists or not ufw_sysctl_check)