--- 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), C(comment) and C(delete). - C(port) must be a positive integer value. - >- C(delete=true) (a boolean) removes the rule the other keys describe instead of adding it. Its comment is not sent, so the rule matches whatever its comment. Deletes are applied after every other rule of the list, so a replacement exists before the old rule goes; a rule that is both added and deleted in the list fails the play, and a delete entry never counts as the SSH allow rule of the lock-out guard. 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 the system_baseline role of this Collection. 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[:]: ). C() 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[:]) 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.