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
84 lines
4.2 KiB
YAML
84 lines
4.2 KiB
YAML
---
|
|
# Rules that must already be stored before the role runs, so converge stays
|
|
# idempotent while still exercising the purge: a second converge finds them
|
|
# deleted (or kept, for the report-only owner) and changes nothing. ufw is
|
|
# installed here; it stays inactive until the role enables it.
|
|
- name: Prepare
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Install ufw
|
|
ansible.builtin.package:
|
|
name: ufw
|
|
state: present
|
|
|
|
# An application profile whose name holds openssh, for the orphan seeds below.
|
|
- name: Define an application profile named like SSH
|
|
ansible.builtin.copy:
|
|
dest: /etc/ufw/applications.d/orphan-openssh
|
|
mode: "0644"
|
|
content: |
|
|
[OrphanOpenSSH]
|
|
title=Orphan test
|
|
description=Test profile for the orphan_rules molecule seeds
|
|
ports=22/tcp
|
|
|
|
- name: Store a manual rule without a tag
|
|
community.general.ufw:
|
|
rule: allow
|
|
port: "7777"
|
|
proto: tcp
|
|
comment: manual
|
|
|
|
# converge.yml replaces this one through a delete entry of the base list that has no
|
|
# comment, so the delete must match the rule without it.
|
|
- name: Store a manual rule that the base list deletes
|
|
community.general.ufw:
|
|
rule: allow
|
|
port: "9091"
|
|
proto: tcp
|
|
comment: replaced
|
|
|
|
# Owners svc_c, svc_d and svc_e keep rules in converge.yml that these entries are
|
|
# not part of: a stale rule to purge, a rule of a removed service, a stale rule
|
|
# that is only reported.
|
|
- name: Store tagged rules that are no longer wanted
|
|
community.general.ufw:
|
|
rule: allow
|
|
port: "{{ ufw_prepare_rule.port | default(omit) }}"
|
|
proto: "{{ 'tcp' if ufw_prepare_rule.port is defined else omit }}"
|
|
from: "{{ ufw_prepare_rule.from | default(omit) }}"
|
|
interface: "{{ ufw_prepare_rule.interface | default(omit) }}"
|
|
direction: "{{ ufw_prepare_rule.direction | default(omit) }}"
|
|
name: "{{ ufw_prepare_rule.name | default(omit) }}"
|
|
comment: "{{ ufw_prepare_rule.comment }}"
|
|
loop:
|
|
# svc_c: the old spec of a rule whose port changed (key of allow|7001|tcp||||in)
|
|
- {port: 7001, comment: "ansible-ufw[svc_c:128c086248]: old"}
|
|
- {port: 7002, comment: "ansible-ufw[svc_d:abcdef0123]: gone"}
|
|
- {port: 7003, comment: "ansible-ufw[svc_e:aaaaaaaaaa]: kept"}
|
|
# svc_h: a stale rule that looks like an SSH rule, which a purge must skip
|
|
- {port: 22, from: 10.9.0.0/16, comment: "ansible-ufw[svc_h:bbbbbbbbbb]: old"}
|
|
# orphan_rules: svc_o1 and svc_o2 are never listed as managed owners in converge.yml.
|
|
# svc_o2's rule looks like the SSH rule and must survive a purge; svc_m is managed
|
|
# but its role is never called, so its rule must survive too.
|
|
- {port: 7401, comment: "ansible-ufw[svc_o1:1111111111]: orphan one"}
|
|
- {port: 7402, comment: "ansible-ufw[svc_o1:2222222222]: orphan two"}
|
|
- {port: 22, from: 10.9.1.0/24, comment: "ansible-ufw[svc_o2:3333333333]: orphan ssh lookalike"}
|
|
- {port: 7501, comment: "ansible-ufw[svc_m:4444444444]: managed, never called"}
|
|
# orphan_rules, owner svc_o3: tagged orphans that may carry SSH and must be kept by a
|
|
# purge: no port (interface only), no port (source only), a range spanning 22, an
|
|
# application profile named like SSH.
|
|
- {interface: tailscale0, direction: in, comment: "ansible-ufw[svc_o3:5555555551]: no port"}
|
|
- {from: 10.20.0.0/24, comment: "ansible-ufw[svc_o3:5555555552]: source only"}
|
|
- {port: "20:30", comment: "ansible-ufw[svc_o3:5555555553]: range over 22"}
|
|
- {name: OrphanOpenSSH, comment: "ansible-ufw[svc_o3:5555555554]: openssh profile"}
|
|
# service_rules purge of svc_p (converge.yml keeps only a rule for 7602): 7601 is stale
|
|
# and deleted, the other three are stale and may carry SSH, so they stay.
|
|
- {port: 7601, comment: "ansible-ufw[svc_p:6666666664]: stale numeric"}
|
|
- {interface: wg0, direction: in, comment: "ansible-ufw[svc_p:6666666661]: no port"}
|
|
- {from: 10.30.0.0/24, comment: "ansible-ufw[svc_p:6666666662]: source only"}
|
|
- {port: "40:50", comment: "ansible-ufw[svc_p:6666666663]: range"}
|
|
loop_control:
|
|
loop_var: ufw_prepare_rule
|