Files
Defame1297andClaude Opus 5.5 43bb2ce8dc
Build / build (pull_request) Successful in 12s
Lint / lint (pull_request) Successful in 28s
Molecule / molecule (pull_request) Canceled after 1m25s
fix(ufw): normalise the conflict check and send deletes without a comment
Review findings on the delete support:

- The both-added-and-deleted check compared the written fields, so
  direction: in or from: any on the added rule hid a conflict that ufw
  sees (it stores no direction as in and no address as any); the delete
  then removed the SSH rule the lock-out guard had counted. Fields are now
  normalised as ufw stores them (incoming/outgoing as in/out too). A
  missing proto stays distinct from tcp, as in ufw.
- A delete sent with a comment that differs from the stored one matches
  nothing in ufw and exits 0, so the rule stayed silently. Deletes are now
  sent without their comment; ufw then matches the rule whatever its
  comment. The scenario's delete entry carries a different comment to
  cover it, and the conflict refusal uses the explicit defaults.
- README: a delete without an address removes the IPv4 and IPv6 rules,
  an IPv4 from allows IPv4 only.
- verify searches 9091/tcp, not a bare 9091 that an owner tag could hold.

Verified locally in check mode against localhost: both bypass lists and
an incoming/outgoing spelling are refused; the planned replacement list
and a no-proto add with a tcp delete pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-11-15 22:24:55 +00:00

26 lines
1.2 KiB
YAML

---
# Rules marked delete: true are applied after every other rule, whatever their place in
# the list, so a replacement rule exists before the rule it replaces is removed. A delete
# is sent without its comment: ufw matches a delete that has no comment against the rule
# whatever its comment, but one with a different comment matches nothing and does nothing.
- name: Apply UFW rules
become: true
community.general.ufw:
rule: "{{ ufw_rule.rule }}"
port: "{{ ufw_rule.port | default(omit) }}"
proto: "{{ ufw_rule.proto | default(omit) }}"
from: "{{ ufw_rule.from | default(omit) }}"
to: "{{ ufw_rule.to | default(omit) }}"
interface: "{{ ufw_rule.interface | default(omit) }}"
direction: "{{ ufw_rule.direction | default(omit) }}"
comment: "{{ omit if ufw_rule.delete | default(false) else ufw_rule.comment | default(omit) }}"
delete: "{{ ufw_rule.delete | default(omit) }}"
loop: "{{ (ufw_aggregated_rules | default([]) | reject('in', ufw_rules_deleted) | list) + ufw_rules_deleted }}"
loop_control:
loop_var: ufw_rule
vars:
ufw_rules_deleted: "{{ ufw_aggregated_rules | default([]) | selectattr('delete', 'defined') | selectattr('delete') | list }}"
when:
- ufw_enabled
- ufw_manage_rules