OPNsense Destination Nat

This role manages Destination NAT (port forwarding) rules on OPNsense via the REST API.

Overview

This role manages Destination NAT (port forwarding) rules on OPNsense via the REST API. It enables forwarding external traffic to internal hosts and redirecting traffic between interfaces. The role provides full lifecycle management: creating new rules, updating existing rules when configuration changes, and deleting orphaned rules that exist on the firewall but are no longer defined in the vars files.

What This Role Does

  1. Fetches existing rules via /api/firewall/d_nat/search_rule
  2. Builds sequence → UUID mapping for idempotency
  3. Creates new rules (sequence doesn’t exist) via /api/firewall/d_nat/addRule
  4. Updates existing rules (sequence exists but fields differ) via /api/firewall/d_nat/setRule
  5. Deletes orphaned rules (sequence exists on firewall but not in vars) via /api/firewall/d_nat/delRule
  6. Applies configuration via /api/firewall/d_nat/apply
  7. Displays summary of configured rules

Role Variables

VariableDescription
vault_opnsense_bjoffrey_user_api_keyOPNsense API key (from vault)
vault_opnsense_bjoffrey_user_api_secretOPNsense API secret (from vault)
opnsense_destination_nat_rulesList of DNAT rules
opnsense_destination_nat_validate_certsValidate SSL certificates

Rule definition fields:

FieldDescription
sequenceUnique number for idempotency (must be unique)
interfaceIncoming interface
destination_portExternal port to match
targetInternal IP to forward to
local_portInternal port to forward to
descriptionRule description
disabled"0" = active, "1" = disabled
protocoltcp, udp, or tcp/udp
logLog matched packets

Notes

  • Removing a rule from the vars list will delete it from the firewall on the next run
  • Changing a sequence number creates a new rule and orphans the old one (which gets deleted)