This is the multi-page printable view of this section. .
Firewall
The nftables role manages the KVM host firewall. It supports Debian-family hosts (Debian, Ubuntu) and RedHat-family hosts (Fedora, Rocky Linux).
On Ubuntu, ufw is installed and enabled on some images; it is disabled by default (nftables_disable_ufw) so the two managers do not fight over the ruleset. On Fedora and Rocky, firewalld is stopped and masked by default (nftables_disable_firewalld) for the same reason.
Usage
nftables_ssh_sourcesmust contain at least one entry unlessnftables_ssh_allow_allis true, and should include the Ansible control node. The role refuses to apply an empty list otherwise. Existing SSH sessions survive the reload (established/related is accepted), but new ones are dropped unless the source is listed.
What the role does
- Validates inputs before touching the host (OS family, IPv4/CIDR format of
nftables_ssh_sources, non-empty SSH ports, set names). - Installs nftables (apt on Debian family, dnf on RedHat family); on Debian family, removes netfilter-persistent / iptables-persistent by default.
- Disables conflicting managers:
ufw --force disableon Ubuntu (when active); stops and masks firewalld on Fedora/Rocky. - Renders the ruleset to
/etc/nftables.conf(Debian family) or/etc/sysconfig/nftables.conf(RedHat family), validated withnft -c -fbefore writing; reloads vianft -fwhen the content changes. - Enables the nftables service at boot.
The rendered config defines two independent tables, inet _filter and inet _nat, deleting and recreating only these on reload — unrelated tables (for example Docker’s) are left untouched.
Variables
| Variable | Description | Default |
|---|---|---|
nftables_ssh_sources |
Source IPv4 addresses/CIDRs allowed to establish SSH | — (required unless allow-all) |
nftables_ssh_allow_all |
Accept SSH from any source | false |
nftables_ssh_ports |
Host SSH port list; list every port sshd listens on | [22] |
nftables_sets |
Named address sets (name + subnet list) |
[] |
nftables_rules |
Rules inserted verbatim into filter_default, filter_forward, nat_prerouting, nat_postrouting |
[] |
filter_forward_default_policy |
FORWARD chain default policy | drop |
net_postrouting_default_policy |
POSTROUTING chain default policy | accept |
nftables_remove_iptables_persistent |
Remove netfilter-persistent / iptables-persistent | true |
nftables_disable_ufw |
Disable ufw when installed and active | true |
nftables_disable_firewalld |
Stop and mask firewalld on RedHat family when installed | true |
Example
Built-in chains
deny— dropnolimit— acceptswitch— kill switch; point rules at it to toggle:
Notes
- With
nftables_setsandnftables_rulesempty, only loopback, established/related traffic, DHCP replies, ICMP and SSH fromnftables_ssh_sourcesare accepted. - The role replaces the distro-shipped
/etc/nftables.confwith its own template. On a host that was already running the distro config, the leftover emptyinet filtertable is removed only when it provably cannot filter traffic (no rules, no drop/reject policies). - On Fedora and Rocky,
nftables.service’sExecReloadbegins withflush ruleset, which would wipe foreign tables. The role therefore reloads by runningnft -f <config>directly instead ofsystemctl reload nftables. - Masking firewalld also blocks D-Bus activation, so
firewall-cmdcannot bring it back. Restore withsystemctl unmask firewalld.