An entry with delete: true (a boolean) removes the rule it describes, passed to community.general.ufw's delete option. It lets a consumer replace a base rule, such as an open SSH allow by narrower ones, which the role could only add before. - rules.yml applies the delete entries after every other rule, whatever their place in the list, so the replacement exists before the old rule goes. - validate.yml: the schema check requires a boolean; a new check refuses a rule the list both adds and deletes (compared without the comment, port as a string), because the delete would win while the lock-out guard counted the add; the lock-out guard no longer counts a delete entry. - service_rules keeps its own key list and does not accept delete. Molecule: prepare stores a commented 9091 rule that the base list deletes with an uncommented entry listed first; verify checks it is gone and the untagged count is unchanged; the idempotence pass covers deleting a rule that is gone. Two new refusals: an SSH rule that is only deleted (lock-out guard) and a rule both added and deleted. Verified locally: validate.yml against localhost in check mode (the planned replacement list passes; the conflict, the delete-only SSH list and a non-boolean delete are refused) and the apply order. Not verified locally: the molecule scenario (CI only), including whether ufw matches the commented rule from an uncommented delete. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
90 lines
4.2 KiB
YAML
90 lines
4.2 KiB
YAML
---
|
|
- name: Validate ufw_rules schema
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_rule.rule is defined
|
|
- ufw_rule.rule in ['allow', 'deny', 'reject', 'limit']
|
|
- ufw_rule.port is not defined or ufw_rule.port | int > 0
|
|
- ufw_rule.proto is not defined or ufw_rule.proto in ['tcp', 'udp']
|
|
- ufw_rule.port is defined or ufw_rule.from is defined or ufw_rule.to is defined or ufw_rule.interface is defined
|
|
- ufw_rule.delete is not defined or ufw_rule.delete is boolean
|
|
fail_msg: "Invalid UFW rule: {{ ufw_rule }}"
|
|
loop: "{{ ufw_aggregated_rules | default([]) }}"
|
|
loop_control:
|
|
loop_var: ufw_rule
|
|
when:
|
|
- ufw_manage_rules
|
|
- ufw_aggregated_rules | length > 0
|
|
|
|
# A rule both added and deleted would be added, then deleted (deletes run last), and the
|
|
# lock-out guard below would still count the added one. The comment is not part of a
|
|
# rule's identity, as in ufw itself; the port is compared as a string.
|
|
- name: Refuse a rule that ufw_rules both adds and deletes
|
|
ansible.builtin.assert:
|
|
that: ufw_rules_conflicting | length == 0
|
|
fail_msg: >-
|
|
These UFW rules are both added and deleted in the same rule list (rule|port|proto|from|to|interface|direction):
|
|
{{ ufw_rules_conflicting }}. Remove one of the two entries.
|
|
vars:
|
|
ufw_rules_conflicting: >-
|
|
{%- set ns = namespace(added=[], deleted=[]) -%}
|
|
{%- for r in ufw_aggregated_rules | default([]) -%}
|
|
{%- set spec = [r.rule | default(''), r.port | default('') | string, r.proto | default(''), r.from | default(''),
|
|
r.to | default(''), r.interface | default(''), r.direction | default('')] | join('|') -%}
|
|
{%- if r.delete | default(false) -%}
|
|
{%- set ns.deleted = ns.deleted + [spec] -%}
|
|
{%- else -%}
|
|
{%- set ns.added = ns.added + [spec] -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{{ ns.deleted | select('in', ns.added) | unique | join(', ') }}
|
|
when:
|
|
- ufw_manage_rules
|
|
- ufw_aggregated_rules | length > 0
|
|
|
|
- name: Validate ufw before.rules schema
|
|
ansible.builtin.assert:
|
|
that:
|
|
- ufw_before_rule.port is not defined or ufw_before_rule.port | int > 0
|
|
- ufw_before_rule.proto is not defined or ufw_before_rule.proto in ['tcp', 'udp']
|
|
- ufw_before_rule.interface is not defined or ufw_before_rule.interface | length > 0
|
|
- ufw_before_rule.port is defined or ufw_before_rule.from is defined or ufw_before_rule.to is defined or ufw_before_rule.interface is defined
|
|
fail_msg: "Invalid UFW before-rule: {{ ufw_before_rule }}"
|
|
loop: "{{ ufw_aggregated_before_rules | default([]) }}"
|
|
loop_control:
|
|
loop_var: ufw_before_rule
|
|
when: ufw_aggregated_before_rules | length > 0
|
|
|
|
# defaults.yml sets the deny policy before rules.yml adds any rule, so a missing
|
|
# SSH rule would lock the host out mid-play. Checks the merged list only; it does
|
|
# not care which variable a rule came from. A missing proto means both protocols
|
|
# and a missing direction means incoming, as in ufw itself. Any `from`, `to` or
|
|
# `interface` restriction still counts: the guard catches a forgotten rule, it
|
|
# cannot know which source address the control host connects from. A rule marked
|
|
# delete: true removes a rule, so it never counts.
|
|
- name: Require an SSH allow rule when incoming traffic is denied by default
|
|
ansible.builtin.assert:
|
|
that: ufw_ssh_rule_count | int > 0
|
|
fail_msg: >-
|
|
No allow/limit rule for TCP port {{ ufw_ssh_port }} (SSH) in the UFW rules, and the
|
|
incoming default is {{ ufw_defaults.incoming }}: enabling it would lock this host out.
|
|
Add the rule, or set ufw_require_ssh_allow: false if SSH is reached another way.
|
|
vars:
|
|
ufw_ssh_rule_count: >-
|
|
{%- set ns = namespace(n=0) -%}
|
|
{%- for r in ufw_aggregated_rules | default([]) -%}
|
|
{%- if r.rule | default('') in ['allow', 'limit']
|
|
and r.port | default('') | string == ufw_ssh_port | string
|
|
and r.proto | default('tcp') == 'tcp'
|
|
and r.direction | default('in') == 'in'
|
|
and not r.delete | default(false) -%}
|
|
{%- set ns.n = ns.n + 1 -%}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{{ ns.n }}
|
|
when:
|
|
- ufw_enabled
|
|
- ufw_manage_rules
|
|
- ufw_require_ssh_allow
|
|
- ufw_defaults.incoming in ['deny', 'reject']
|