Files
Kamino/roles/ufw/tasks/validate.yml
T
Defame1297andClaude Opus 5.5 e18db424ec
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Failing after 25s
Molecule / molecule (pull_request) Failing after 5m42s
feat(ufw): add the ufw role
Copied from the environment repository's local role. The files are
identical except: galaxy namespace mortis; comments, argument_specs
descriptions and the README scrubbed of environment references (issue
numbers, CI runs, playbooks, inventory paths and groups, scripts and
caller roles by name), with the technical explanation kept and reworded
generically; the stray-variable guard's fail message drops its issue
reference; the molecule scenario calls the role by its FQCN
(mortis.kamino.ufw), follows the Kamino scenario conventions
(kamino-molecule header, scenario.test_sequence) and no longer sets
ANSIBLE_COLLECTIONS_PATH or ANSIBLE_ROLES_PATH. No variable, default,
argument_specs type or task logic changed; the test coverage is the same.

The README follows the other Kamino roles: purpose, the main,
service_rules and orphan_rules entry points, variables, and example
plays with the FQCN and tasks_from. The meta licence stays
GPL-3.0-or-later AND GPL-3.0-only (templates/before.rules.j2 is derived
from ufw's own file, already annotated in REUSE.toml and NOTICE).
galaxy.yml's dependency comment names ufw as a community.general user.

Not verified locally: the molecule scenario (it runs on the
kamino-molecule runner in CI) and ansible-test sanity.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
2026-11-14 16:43:29 +00:00

61 lines
2.8 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
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
- 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.
- 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' -%}
{%- 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']