molecule's idempotence step reruns converge.yml, so plays that only prove a role refuses bad input ran twice and added nothing the second time. The ufw, sshd, gitea and traefik scenarios now keep only the plays that apply the role in converge.yml; the refusal plays and gitea's child ansible-playbook guard checks run once, first in verify.yml. Every existing assertion runs after them, so verify still proves a refused run left the host unchanged. Assisted-by: Claude Code:claude-opus-5-5
192 lines
6.8 KiB
YAML
192 lines
6.8 KiB
YAML
---
|
|
# Only runs that apply the role; the refusal cases are in verify.yml.
|
|
- name: Converge with an SSH rule and service rules
|
|
hosts: all
|
|
gather_facts: true
|
|
roles:
|
|
- role: mortis.kamino.ufw
|
|
vars:
|
|
ufw_rules:
|
|
# Listed first on purpose: deletes run after every other rule. The second run
|
|
# deletes a rule that is gone, which must not report a change.
|
|
# The comment differs from the stored one and is not sent, so it still matches.
|
|
- {rule: allow, port: 9091, proto: tcp, comment: other, delete: true}
|
|
- {rule: allow, port: 22, proto: tcp, comment: ssh}
|
|
- {rule: allow, port: 8080, proto: tcp, from: 10.0.0.0/8, comment: web}
|
|
- {rule: allow, port: 53, proto: udp, comment: dns}
|
|
# no proto: both protocols, and the guard must cope with the key missing
|
|
- {rule: allow, port: 9090, comment: metrics}
|
|
|
|
# The service_rules entry point. prepare.yml stored a stale rule for svc_c,
|
|
# a rule of the removed service svc_d and a stale rule for svc_e, so every pass here
|
|
# is a no-op after the first one, purge included.
|
|
- name: Converge the rules of several services through the entry point
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Two rules for svc_a
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_a
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 8001, proto: tcp, comment: alpha web}
|
|
- {rule: allow, port: 8002, proto: udp, from: 10.1.0.0/16, comment: alpha dns}
|
|
|
|
- name: Two rules for svc_b, one without a comment
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_b
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 8003, proto: tcp, comment: beta}
|
|
- {rule: deny, port: 8004}
|
|
|
|
- name: Keep one rule for svc_c and purge its stale rule
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_c
|
|
ufw_purge_stale: true
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 7101, proto: tcp, comment: gamma}
|
|
|
|
- name: Remove svc_d, which deletes without ufw_purge_stale
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_d
|
|
ufw_service_state: absent
|
|
|
|
- name: Keep the stale rule of svc_e because purging is off
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_e
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 7201, proto: tcp, comment: epsilon}
|
|
|
|
- name: A comment that mentions the SSH port on another port
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_g
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 8022, proto: tcp, comment: forward to 22 here}
|
|
|
|
- name: Purge svc_h without touching its stale rule that looks like the SSH rule
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_h
|
|
ufw_purge_stale: true
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 7301, proto: tcp, comment: eta}
|
|
|
|
- name: Purge svc_p but keep its stale rules that may carry SSH
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
# prepare.yml stored four stale rules for svc_p: a numeric port (deleted), and a rule
|
|
# with no port, a source-only rule and a range, which are kept as possible SSH paths.
|
|
- name: Keep one rule for svc_p and purge its stale rules
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: service_rules
|
|
vars:
|
|
ufw_service_name: svc_p
|
|
ufw_purge_stale: true
|
|
ufw_service_rules:
|
|
- {rule: allow, port: 7602, proto: tcp, comment: desired}
|
|
|
|
# The orphan_rules entry point. prepare.yml stored two rules of svc_o1, a rule of
|
|
# svc_o2 that looks like the SSH rule, and a rule of svc_m. The managed owners below are
|
|
# the ones used above plus svc_m (listed but never called), so svc_o1 and svc_o2 are
|
|
# orphans. The cases that must change nothing run first and the purge last, so the
|
|
# idempotence run finds nothing left to delete. Check mode is not exercised: whether it
|
|
# reaches the tasks of an include_role is not verified.
|
|
- name: Report and purge the rules of unmanaged owners
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
ufw_converge_managed:
|
|
- svc_a
|
|
- svc_b
|
|
- svc_c
|
|
- svc_d
|
|
- svc_e
|
|
- svc_g
|
|
- svc_h
|
|
- svc_m
|
|
- svc_p
|
|
tasks:
|
|
# The marker is written after the purge, so the expected counts are absolute on both
|
|
# passes: svc_o1 holds 2 rules before the purge and 0 once the marker exists.
|
|
- name: Look for the marker of an earlier purge
|
|
ansible.builtin.stat:
|
|
path: /var/tmp/ufw-orphans-purged
|
|
register: ufw_conv_marker
|
|
|
|
- name: Work out the expected number of rules of svc_o1
|
|
ansible.builtin.set_fact:
|
|
ufw_conv_o1_initial: "{{ 0 if ufw_conv_marker.stat.exists else 2 }}"
|
|
|
|
- name: Assert it before the first case
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
|
|
- name: No managed owners given, purge requested
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: orphan_rules
|
|
vars:
|
|
ufw_purge_orphans: true
|
|
|
|
- name: Nothing was deleted without a list
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
|
|
- name: An empty list of managed owners, purge requested
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: orphan_rules
|
|
vars:
|
|
ufw_managed_owners: []
|
|
ufw_purge_orphans: true
|
|
|
|
- name: Nothing was deleted with an empty list
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
|
|
- name: Report the orphans only
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: orphan_rules
|
|
vars:
|
|
ufw_managed_owners: "{{ ufw_converge_managed }}"
|
|
ufw_purge_orphans: false
|
|
|
|
- name: Reporting deleted nothing
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
|
|
- name: Purge the orphans
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.ufw
|
|
tasks_from: orphan_rules
|
|
vars:
|
|
ufw_managed_owners: "{{ ufw_converge_managed }}"
|
|
ufw_purge_orphans: true
|
|
|
|
- name: Only svc_o1 is gone, the kept orphans and svc_m stay
|
|
ansible.builtin.include_tasks: tasks/orphan_check.yml
|
|
vars:
|
|
ufw_conv_o1_want: 0
|
|
|
|
- name: Mark the purge as done
|
|
ansible.builtin.command: touch /var/tmp/ufw-orphans-purged
|
|
changed_when: false
|