--- # 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 # 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