Skip to content

Commit

Permalink
Add support for wireguard_include_peers variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Nov 6, 2024
1 parent 2b65ab5 commit ad6a9f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ wireguard_unmanaged_peers:
persistent_keepalive: 0
```

If present, the ``wireguard_include_peers`` setting restricts which peers (as hostnames) are included for a particular host. This can be useful when not all nodes can reach each other:

```yaml
wireguard_include_peers:
- client.example.com
```

One of `wireguard_address` (deprecated) or `wireguard_addresses` (recommended) is required as already mentioned. It's the IPs of the interface name defined with `wireguard_interface` variable (`wg0` by default). Every host needs at least one unique VPN IP of course. If you don't set `wireguard_endpoint` the playbook will use the hostname defined in the `vpn` hosts group (the Ansible inventory hostname). If you set `wireguard_endpoint` to `""` (empty string) that peer won't have a endpoint. That means that this host can only access hosts that have a `wireguard_endpoint`. That's useful for clients that don't expose any services to the VPN and only want to access services on other hosts. So if you only define one host with `wireguard_endpoint` set and all other hosts have `wireguard_endpoint` set to `""` (empty string) that basically means you've only clients besides one which in that case is the WireGuard server. The third possibility is to set `wireguard_endpoint` to some hostname. E.g. if you have different hostnames for the private and public DNS of that host and need different DNS entries for that case setting `wireguard_endpoint` becomes handy. Take for example the IP above: `wireguard_address: "10.8.0.101"`. That's a private IP and I've created a DNS entry for that private IP like `host01.i.domain.tld` (`i` for internal in that case). For the public IP I've created a DNS entry like `host01.p.domain.tld` (`p` for public). The `wireguard_endpoint` needs to be a interface that the other members in the `vpn` group can connect to. So in that case I would set `wireguard_endpoint` to `host01.p.domain.tld` because WireGuard normally needs to be able to connect to the public IP of the other host(s).

## Example
Expand Down
7 changes: 6 additions & 1 deletion templates/etc/wireguard/wg.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ PostDown = {{ wg_postdown }}
SaveConfig = {{ wireguard_save_config }}
{% endif %}
{% for host in ansible_play_hosts %}
{% if host != inventory_hostname and ((hostvars[host].wireguard_endpoint is defined and hostvars[host].wireguard_endpoint != "") or (wireguard_endpoint is defined and wireguard_endpoint != "")) %}
{% if host != inventory_hostname
and ((hostvars[host].wireguard_endpoint is defined and hostvars[host].wireguard_endpoint != "") or (wireguard_endpoint is defined and wireguard_endpoint != ""))
and (wireguard_include_peers is not defined or host in wireguard_include_peers)
%}

[Peer]
# Name = {{ host }}
Expand Down Expand Up @@ -106,6 +109,7 @@ Endpoint = {{host}}:{{wireguard_port}}

# Peers not managed by Ansible from "wireguard_unmanaged_peers" variable
{% for peer in wireguard_unmanaged_peers.keys() %}
{% if wireguard_include_peers is not defined or peer in wireguard_include_peers %}
[Peer]
# Name = {{ peer }}
PublicKey = {{ wireguard_unmanaged_peers[peer].public_key }}
Expand All @@ -121,5 +125,6 @@ Endpoint = {{ wireguard_unmanaged_peers[peer].endpoint }}
{% if wireguard_unmanaged_peers[peer].persistent_keepalive is defined %}
PersistentKeepalive = {{ wireguard_unmanaged_peers[peer].persistent_keepalive }}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}

0 comments on commit ad6a9f2

Please sign in to comment.