Files
Kamino/roles/ufw/meta/argument_specs.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

252 lines
13 KiB
YAML

---
argument_specs:
main:
short_description: Install and configure UFW
description:
- >-
Installs ufw, applies sysctl overrides, reads the base C(ufw_rules) and C(ufw_before_rules) lists (a defined
variable named C(*_ufw_rules) or C(*_ufw_before_rules) fails the play, service roles use the
C(service_rules) entry point), then sets default policies, rules and enables UFW.
- >-
Refuses to run when the incoming default is deny or reject and no allow or limit rule for the SSH TCP port exists
in C(ufw_rules). Only rules with C(direction=in) or no C(direction) count; C(direction=incoming), which
the module accepts, does not.
options:
ufw_enabled:
type: bool
default: true
description:
- When false every task that touches UFW is skipped, except installing the package.
ufw_manage_rules:
type: bool
default: true
description:
- >-
When false the C(ufw_rules) schema check, the SSH lock-out guard and rule
application are skipped. Before-rules validation, C(before.rules), default policies and
enabling still run, gated on C(ufw_enabled).
ufw_defaults:
type: dict
description:
- Default policies applied before any rule. The role default is C(incoming=deny), C(outgoing=allow), C(routed=deny).
- All three keys must be present when this is overridden, because each is read unconditionally.
options:
incoming:
type: str
choices: [allow, deny, reject]
description: Default policy for incoming traffic.
outgoing:
type: str
choices: [allow, deny, reject]
description: Default policy for outgoing traffic.
routed:
type: str
choices: [allow, deny, reject]
description: Default policy for routed (forwarded) traffic.
ufw_logging:
type: str
default: "on"
choices: ["on", "off", low, medium, high, full]
description:
- UFW logging setting. UFW is only re-enabled when it is inactive or its current logging line differs from this value.
ufw_require_ssh_allow:
type: bool
default: true
description:
- Enables the lock-out guard. Set false only when the host is reached without SSH on C(ufw_ssh_port).
ufw_ssh_port:
type: raw
description:
- TCP port the lock-out guard looks for, compared as a string. The role default is the Jinja expression C(ansible_port | default(22, true)).
ufw_rules:
type: raw
description:
- Base list of rules, the only list the main entry applies. The role default is an empty list.
- >-
Each item is a mapping with C(rule) (allow, deny, reject or limit) and at least one of C(port), C(from), C(to)
or C(interface). Optional keys are C(proto) (tcp or udp), C(direction) and C(comment).
- C(port) must be a positive integer value.
ufw_before_rules:
type: raw
description:
- >-
Base list of ACCEPT entries written into /etc/ufw/before.rules, the only source of entries (no role contributes
before-rules). The role default is an empty list.
- >-
Each item is a mapping with at least one of C(port), C(from), C(to) or C(interface). Optional keys are
C(proto) (tcp or udp) and C(comment). Only C(interface), C(proto), C(port) and C(from) are written to the
file.
ufw_sysctl_overrides:
type: raw
description:
- >-
List of C({name, value}) mappings written as C(name=value) into /etc/ufw/sysctl.conf, so UFW does not reset
values set by mortis.kamino.system_baseline. The role default is an empty list.
- C(name) uses slashes, for example C(net/ipv4/conf/all/log_martians).
service_rules:
short_description: Apply the firewall rules of one service, tagged with its name
description:
- >-
Entry point for a service role that owns firewall rules, used with C(include_role) and C(tasks_from=service_rules).
The main entry must have run before: this entry point installs nothing and touches no default policy, no
before.rules and no base rule.
- >-
Each rule is applied with the comment C(ansible-ufw[<owner>:<key>]: <text>). C(<key>) is the first ten hex
characters of the sha1 of the rule's spec (rule, port, proto, from, to, interface, direction), so a changed spec
is a new rule and the old one becomes stale. Stale rules (tagged with this owner, key no longer wanted) are
reported, and deleted when C(ufw_purge_stale) is true or the state is C(absent).
- >-
A stale line is deleted only when it is an explicit single numeric port other than C(ufw_ssh_port); a line with no
port, a port range or list, C(ssh) or C(openssh) outside the comment, or one the SSH regex matches may open SSH
and is kept.
- >-
Rules without a tag (base rules, the SSH rule, manual rules) are never read as stale and never deleted. The play
fails before changing anything when a rule is listed twice, when another owner already holds the same spec, or
when a rule would allow the SSH port (C(ufw_ssh_port), TCP, incoming), because that would retag the base SSH rule.
options:
ufw_enabled:
type: bool
default: true
description:
- When false nothing is applied and a debug message says so.
ufw_manage_rules:
type: bool
default: true
description:
- When false nothing is applied and a debug message says so.
ufw_ssh_port:
type: raw
description:
- TCP port whose rule is never retagged or deleted, compared as a string. The role default is C(ansible_port | default(22, true)).
ufw_service_name:
type: str
required: true
description:
- >-
Owner of the rules, lower-case letters, digits and underscores (C(^[a-z0-9_]+$), asserted in the tasks).
C(base) is reserved.
ufw_service_rules:
type: raw
description:
- >-
List of rule mappings with the keys C(ufw_rules) accepts: C(rule) (allow, deny, reject or limit), at least
one of C(port), C(from), C(to) or C(interface), and optional C(proto), C(direction) and C(comment). Other keys are
refused, and C(interface) needs C(direction).
Comments are printable ASCII without a single quote or backslash.
- Required when C(ufw_service_state) is C(present); an empty list makes every rule of the owner stale. Ignored for C(absent).
- Typed raw because callers pass a templated variable. It is asserted to be a list.
ufw_service_state:
type: str
default: present
choices: [present, absent]
description:
- >-
C(absent) makes the desired set empty, so every tagged rule of the owner is stale, and it is an explicit
removal request: they are deleted whatever C(ufw_purge_stale) says (not in check mode, never an SSH-like line).
ufw_purge_stale:
type: bool
default: false
description:
- >-
Only for C(ufw_service_state=present). When true, stale rules of this owner are deleted after every desired rule
was applied (never in check mode, never a rule that looks like the SSH rule), then the result is verified. When
false they are only reported. True with an empty C(ufw_service_rules) is refused: use C(absent) to remove all.
ufw_tag_prefix:
type: str
default: ansible-ufw
description:
- Prefix of the owner tag. Must agree with C(ufw_tag_regex); changing it orphans rules already tagged.
ufw_tag_regex:
type: str
default: "comment 'ansible-ufw\\[([a-z0-9_]+):([0-9a-f]{10})\\]:"
description:
- Regular expression with two groups (owner, key) that finds the tag in a line of C(ufw show added).
ufw_tag_comment_regex:
type: str
default: '^[] -&(-[^-~]*\Z'
description:
- Regular expression a rule comment must match.
ufw_port_token_regex:
type: str
description:
- >-
Regular expression that finds the port tokens (a number, a range such as C(20:30) or a list such as
C(22,80)) of a rule line without its comment. A line is deleted only with exactly one plain numeric
token other than C(ufw_ssh_port). See the role defaults.
ufw_reserved_owners:
type: list
elements: str
default: [base]
description:
- Owner names a service may not use.
orphan_rules:
short_description: Report, and optionally delete, the tagged rules of owners that are no longer managed
description:
- >-
Entry point used with C(include_role) and C(tasks_from=orphan_rules), after every service role of the host ran.
An orphan is a rule tagged C(ansible-ufw[<owner>:<key>]) whose owner is neither in C(ufw_managed_owners) nor in
C(ufw_reserved_owners). It is what a service removed from the host leaves behind, because C(service_rules) is
never called for that owner again.
- >-
Fails closed: when C(ufw_managed_owners) is undefined or empty nothing is read, reported or deleted. Orphans are
reported; they are deleted only when C(ufw_purge_orphans) is true, never in check mode and never a line that looks
like the SSH rule. Untagged rules (base, SSH, manual), rules of managed or reserved owners are never touched.
A tagged orphan line is deleted only when it is an explicit single numeric port other than C(ufw_ssh_port);
a line with no port, a port range or list, C(ssh) or C(openssh) outside the comment, or one the SSH regex matches
may open SSH and is kept and reported. Lines with unexpected characters are kept and only their owner and key are
reported. The owner is the C(ufw_service_name) a role passes: a role called by another role that calls
C(service_rules) under its own name is reported as an orphan unless that name is in the list.
After a delete the rules are read again and the play fails unless exactly the deletable orphan lines are gone
and every other rule, SSH included, is unchanged (kept lines stay and are reported). Skipped with a debug message when ufw is not active.
options:
ufw_enabled:
type: bool
default: true
description:
- When false nothing is looked at and a debug message says so.
ufw_manage_rules:
type: bool
default: true
description:
- When false nothing is looked at and a debug message says so.
ufw_ssh_port:
type: raw
description:
- TCP port whose rule is never deleted, compared as a string. The role default is C(ansible_port | default(22, true)).
ufw_managed_owners:
type: list
elements: str
description:
- >-
Owner names that are still managed, each C(^[a-z0-9_]+$) (asserted in the tasks). No default: undefined or empty
makes the entry point do nothing. When a play derives the owners from a per-host list of service roles, each owner name must equal the
role name in that list, which the service passes as C(ufw_service_name).
ufw_purge_orphans:
type: bool
default: false
description:
- When true the orphaned rules are deleted (not in check mode, never an SSH-like line). When false they are only reported.
ufw_tag_prefix:
type: str
default: ansible-ufw
description:
- Prefix of the owner tag. Must agree with C(ufw_tag_regex).
ufw_tag_regex:
type: str
default: "comment 'ansible-ufw\\[([a-z0-9_]+):([0-9a-f]{10})\\]:"
description:
- Regular expression with two groups (owner, key) that finds the tag in a line of C(ufw show added).
ufw_port_token_regex:
type: str
description:
- >-
Regular expression that finds the port tokens (a number, a range such as C(20:30) or a list such as
C(22,80)) of a rule line without its comment. A line is deleted only with exactly one plain numeric
token other than C(ufw_ssh_port). See the role defaults.
ufw_reserved_owners:
type: list
elements: str
default: [base]
description:
- Owner names that are never orphans.