community.openwrt.wg_interface module – Manage WireGuard/AmneziaWG identity keys, interface UCI sections and WAN ingress firewall rules
Note
This module is part of the community.openwrt collection (version 1.11.0).
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.openwrt.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: community.openwrt.wg_interface.
New in community.openwrt 1.10.0
Synopsis
Manages WireGuard/AmneziaWG identity keys, interface UCI sections and the WAN ingress firewall rule for each interface’s listen port.
The module performs the following steps in order:
Determine the protocol of the ubus key server from the interfaces being kept (
wireguardby default), failing if they request different protocols.
Resolve the host identity key, reading it from every interface in
managed_interfaces, failing on a hard mismatch (two distinct keys), otherwise reusing the single existing key or generating a fresh one via ubus, and deriving and reporting the public key.
For each entry in
interfaces, remove it when itsinterfaces[].stateorstateisabsent, dropping the interface section, its peer sections and its WAN ingress rule (unlessmanage_firewall=false). Otherwise write its UCI section, with the identity key when it is inmanaged_interfaces(a fresh key onforce_rekey=true) and the AWG options when its protocol isamneziawg, and ensure the WAN ingress ACCEPT rule for its listen port, or remove it wheninterfaces[].listen_portis not set (unlessmanage_firewall=false).
Write the identity key to the other interfaces in
managed_interfacesthat miss one, or to all of them onforce_rekey=true; names with no interface section yet are skipped.
For the available WireGuard UCI config options see https://openwrt.org/docs/guide-user/services/vpn/wireguard/start.
AmneziaWG support relies on the
amneziawgubus key server; see Requirements below.
Note
This module has a corresponding action plugin.
Requirements
The below requirements are needed on the host that executes this module.
rpcdmust be running (default behavior in standard OpenWrt).rpcd-mod-wireguard.The custom module
rpcd-mod-amneziawgfor AmneziaWG, from https://github.com/vooon/wgsd-ucode/tree/main/rpcd-mod-amneziawg.
Parameters
Parameter |
Comments |
|---|---|
Ucode template for the UCI section id of the WAN ingress firewall rule. Rendered with these variables in scope: Avoid using Default: |
|
Ucode template for the Rendered with these variables in scope: Default: |
|
Regenerate every peer PSK even if one already exists on-device. When Choices:
|
|
Regenerate the identity key even if one exists. When Choices:
|
|
List of interface configurations to (re)write. Each suboption below sets the UCI option of the same name. When a suboption is not set, that option is removed from an existing interface section, so options set by other means are lost. Other options of the section are left alone. |
|
List of addresses with prefix (v4/v6). |
|
AmneziaWG tuning and 3.x/3.1 options ( Each See https://docs.amnezia.org/documentation/amnezia-wg for what each parameter does and the values it accepts. That handler knows these keys: integers Those keys that are not set are removed from the interface section. Other keys are written as given, so options of other AmneziaWG implementations can be set too. Only meaningful when the interface protocol is When an interface is switched to |
|
List of DNS servers advertised for the interface (OpenWrt |
|
DNS metric for the interface (OpenWrt |
|
Interface listen port. |
|
Interface metric. |
|
Interface MTU. |
|
Interface name. |
|
Whether to accept DNS servers provided by this interface (OpenWrt Choices:
|
|
Interface protocol. Defaults to the module-wide Choices:
|
|
Whether the interface should be present or absent. When Defaults to the module-wide Choices:
|
|
Whether to (re)write the WAN ingress ACCEPT rule for each interface listen port, and remove it along with the interface or when Set to Choices:
|
|
Names of all the interfaces on the host that share the host identity key. The module reads the private key from each of them, fails if they hold different keys (unless Names that have no interface section yet are skipped; they get the key when they are configured. Use it when When not given, it is derived from A run that only removes interfaces, with this option not given, does no key management. |
|
Ucode template for the peer UCI section id, used to locate a reusable on-device pre-shared key for idempotency. Rendered with these variables in scope: Must render the same id as Default: |
|
Optional list of peer section skeletons. For each, the module resolves (but does not store) the pre-shared key: it reuses an existing on-device PSK or generates one with the ubus Resolved keys are returned in |
|
Regenerate this peer PSK even if one already exists. Choices:
|
|
Parent interface name. |
|
Peer protocol, used to render Defaults to the Choices:
|
|
Peer section suffix, available as |
|
Module-wide protocol, used as the default for every entry in An explicitly-set Choices:
|
|
Module-wide state, used as the default for every entry in An explicitly-set Choices:
|
Attributes
Attribute |
Support |
Description |
|---|---|---|
Support: full |
Can run in |
|
Support: full |
Returns details on what has changed (or possibly needs changing in |
|
Platform: OpenWrt |
Target platform for this module. |
Notes
Note
The module writes UCI configuration only; it does not bring the interface up nor establish tunnels.
The identity key and peer PSKs are generated via ubus; the
wireguardobject is used as a fallback whenever the call to theamneziawgobject fails or returns no result.It is an intended design decision that all managed interfaces on a host share the same private key (and therefore the same public key), while each peer gets its own distinct pre-shared key. This keeps monitoring and peer mapping simple. The host is identified by one public key, and every peer link is identified by its unique PSK.
To give some interfaces a key of their own, configure them in a separate task, without listing them in
managed_interfacesof the other tasks: each task then resolves the key of its own interfaces only. The module never takes a private key as a parameter, so that private keys never leave the device. To set a specific private key, use community.openwrt.uci on theprivate_keyoption of the interface.psksholds the pre-shared keys in clear text. Set theno_logtask keyword on tasks withpeersto keep them out of the output and logs.Template options (
*_template) use ucode’s native template engine (Jinja-style{{ ... }}/{% ... %}).This module does not work on OpenWrt 23.05, which has no
rpcd-mod-wireguardpackage and therefore nowireguardubus object.
Examples
# Hub: resolve/write the identity key and a hub interface section, then
# generate a per-spoke peer PSK for each of the two spokes
- community.openwrt.wg_interface:
managed_interfaces: [wg0]
interfaces:
- name: wg0
proto: wireguard
listen_port: 51820
addresses:
- "10.8.0.1/24"
peers:
- iface: wg0
section: spoke1
- iface: wg0
section: spoke2
# Two independent p2p links on one host: each link is its own interface with
# its own listen port, identity key (shared host-wide) and per-link peer PSK
- community.openwrt.wg_interface:
managed_interfaces: [wg_l1, wg_l2]
interfaces:
- name: wg_l1
proto: wireguard
listen_port: 51821
addresses:
- "10.9.0.1/30"
- name: wg_l2
proto: wireguard
listen_port: 51822
addresses:
- "10.10.0.1/30"
peers:
- iface: wg_l1
- iface: wg_l2
# AmneziaWG hub. The tuning values below only show the syntax: generate your own,
# and use the same ones on every end of the link.
# See https://docs.amnezia.org/documentation/amnezia-wg
- community.openwrt.wg_interface:
proto: amneziawg
managed_interfaces: [awg0]
interfaces:
- name: awg0
listen_port: 51830
addresses:
- "10.11.0.1/24"
awg_tuning:
awg_jc: 4
awg_jmin: 40
awg_jmax: 70
awg_s1: 52
awg_s2: 16
awg_h1: 1166528911
awg_h2: 2007365624
awg_h3: 3125367892
awg_h4: 4069137714
peers:
- iface: awg0
section: spoke1
# WireGuard and AmneziaWG interfaces on one host, sharing the host identity key:
# one task per protocol, both listing every interface in managed_interfaces
- community.openwrt.wg_interface:
proto: wireguard
managed_interfaces: [wg0, awg0]
interfaces:
- name: wg0
listen_port: 51820
addresses:
- "10.8.0.1/24"
- community.openwrt.wg_interface:
proto: amneziawg
managed_interfaces: [wg0, awg0]
interfaces:
- name: awg0
listen_port: 51830
addresses:
- "10.11.0.1/24"
awg_tuning:
awg_jc: 4
awg_jmin: 40
awg_jmax: 70
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
|---|---|
True if any WAN ingress firewall rule was created, modified or removed. Returned: always |
|
Names of (re)written interface sections. Returned: always |
|
Host identity public key. Empty when no key is managed, in a run that only removes interfaces without Returned: always |
|
Names of interfaces removed because their Returned: always |