feat(ansible): add VPN node configuration automation

This commit is contained in:
2026-06-10 12:36:27 +02:00
parent 89efd6430d
commit 5e6d4c1479
32 changed files with 526 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
---
+18
View File
@@ -0,0 +1,18 @@
---
- name: Update apt cache
apt:
update_cache: true
cache_valid_time: 3600
- name: Upgrade packages
apt:
upgrade: dist
- name: Install common packages
apt:
name: "{{ common_packages }}"
state: present
- name: Configure timezone
timezone:
name: "{{ timezone }}"
+38
View File
@@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).
+2
View File
@@ -0,0 +1,2 @@
---
# defaults file for roles/networking
+4
View File
@@ -0,0 +1,4 @@
---
# handlers file for roles/networking
- name: reload sysctl
command: sysctl --system
+19
View File
@@ -0,0 +1,19 @@
---
- name: Ensure bridge-utils is installed
apt:
name:
- bridge-utils
- vlan
- iproute2
state: present
- name: Create bridge if missing
command: ip link add br-main-vpn type bridge
register: bridge_create
failed_when: false
changed_when: bridge_create.rc == 0
- name: Bring bridge up
command: ip link set br-main-vpn up
failed_when: false
changed_when: false
+16
View File
@@ -0,0 +1,16 @@
---
# tasks file for roles/networking
- name: Deploy networking sysctl configuration
template:
src: 99-openvpn.conf.j2
dest: /etc/sysctl.d/99-openvpn.conf
owner: root
group: root
mode: "0644"
notify: reload sysctl
- name: Apply sysctl immediately
command: sysctl --system
changed_when: false
- import_tasks: bridge.yml
@@ -0,0 +1,6 @@
net.ipv4.ip_forward=1
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.enp7s0.rp_filter=0
+38
View File
@@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).
+17
View File
@@ -0,0 +1,17 @@
openvpn_port: 1194
openvpn_proto: tcp
openvpn_mode: tap0
openvpn_ccd_dir: /etc/openvpn/ccd
openvpn_cert_dir: /etc/openvpn/certs
openvpn_config_dir: /etc/openvpn/server
openvpn_service: openvpn-server@server
radius_server: 10.1.0.101
radius_auth_port: 1812
radius_acct_port: 1813
radius_shared_secret: aloulou
nas_identifier: "{{ inventory_hostname }}"
nas_ip_address: "{{ private_ip }}"
+23
View File
@@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID3DCCAsSgAwIBAgIIIWlpje69/KowDQYJKoZIhvcNAQELBQAwdDELMAkGA1UE
BhMCREUxCzAJBgNVBAYTAkRFMRAwDgYDVQQIEwdCYXZhcmlhMQ8wDQYDVQQHEwZN
dW5pY2gxEjAQBgNVBAoTCU15Q29tcGFueTEMMAoGA1U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dWyCITsNf
WVBGoDNIW3jbOD5/hPS8TfFvAVsRggMQTPSiMr5Z+pLrIoj45VkO
-----END RSA PRIVATE KEY-----
+10
View File
@@ -0,0 +1,10 @@
#!/bin/bash
TENANT="${common_name#*@}"
VLAN_ID=$(( ( $(echo -n "$TENANT" | cksum | cut -d' ' -f1) % 3000 ) + 100 ))
if [ -n "$VLAN_ID" ]; then
echo "vlan-pvid $VLAN_ID" >> "$client_connect_config_file"
fi
exit 0
+5
View File
@@ -0,0 +1,5 @@
---
- name: restart openvpn
systemd:
name: "{{ openvpn_service }}"
state: restarted
+19
View File
@@ -0,0 +1,19 @@
---
- name: Start OpenVPN
systemd:
name: "{{ openvpn_service }}"
state: started
enabled: true
- name: Wait for tap0
command: ip link show tap0
register: tap_check
retries: 10
delay: 2
until: tap_check.rc == 0
- name: Bring tap0 up
command: ip link set tap0 up
- name: Attach tap0 to bridge
command: ip link set tap0 master br-main-vpn
+32
View File
@@ -0,0 +1,32 @@
---
- name: Create certificate directory
file:
path: "{{ openvpn_cert_dir }}"
state: directory
owner: root
group: root
mode: "0755"
- name: Deploy CA certificate
copy:
src: certs/ca.crt
dest: "{{ openvpn_cert_dir }}/ca.crt"
owner: root
group: root
mode: "0644"
- name: Deploy server certificate
copy:
src: certs/server.crt
dest: "{{ openvpn_cert_dir }}/server.crt"
owner: root
group: root
mode: "0644"
- name: Deploy server private key
copy:
src: certs/server.key
dest: "{{ openvpn_cert_dir }}/server.key"
owner: root
group: root
mode: "0600"
+31
View File
@@ -0,0 +1,31 @@
---
- name: Deploy OpenVPN configuration
template:
src: server.conf.j2
dest: /etc/openvpn/server/server.conf
owner: root
group: root
mode: "0644"
notify: restart openvpn
- name: Deploy Radius configuration
template:
src: radiusplugin.cnf.j2
dest: /etc/openvpn/radiusplugin.cnf
owner: root
group: root
mode: "0600"
notify: restart openvpn
- name: Deploy client-connect script
copy:
src: client-connect.sh
dest: /etc/openvpn/scripts/client-connect.sh
owner: root
group: root
mode: "0755"
- name: Enable OpenVPN service
systemd:
name: openvpn@server
enabled: true
+20
View File
@@ -0,0 +1,20 @@
---
- name: Install OpenVPN packages
apt:
name:
- openvpn
- openvpn-auth-radius
state: present
update_cache: true
- name: Create OpenVPN directories
file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: "0755"
loop:
- "{{ openvpn_ccd_dir }}"
- "{{ openvpn_config_dir }}"
- /etc/openvpn/scripts
+5
View File
@@ -0,0 +1,5 @@
---
- import_tasks: install.yml
- import_tasks: certs.yml
- import_tasks: config.yml
- import_tasks: bridge-tap.yml
@@ -0,0 +1,31 @@
NAS-Identifier={{ nas_identifier }}
NAS-IP-Address={{ nas_ip_address }}
Service-Type=5
Framed-Protocol=1
NAS-Port-Type=5
OpenVPNConfig=/etc/openvpn/server/server.conf
subnet=255.255.255.0
useauthcontrolfile=true
useclientconnectdeferfile=true
overwriteccfiles=true
nonfatalaccounting=false
AddMessageAuthenticator=yes
log_level=5
server
{
name={{ radius_server }}
authport={{ radius_auth_port }}
acctport={{ radius_acct_port }}
sharedsecret={{ radius_shared_secret }}
retry=3
wait=10
}
+45
View File
@@ -0,0 +1,45 @@
port {{ openvpn_port }}
proto {{ openvpn_proto }}
dev {{ openvpn_mode }}
client-to-client
persist-key
persist-tun
keepalive 10 120
server-bridge
mode server
vlan-tagging
plugin /usr/lib/openvpn/radiusplugin.so /etc/openvpn/radiusplugin.cnf
verify-client-cert none
username-as-common-name
client-config-dir {{ openvpn_ccd_dir }}
script-security 2
client-connect /etc/openvpn/scripts/client-connect.sh
ca {{ openvpn_cert_dir }}/ca.crt
cert {{ openvpn_cert_dir }}/server.crt
key {{ openvpn_cert_dir }}/server.key
dh none
ecdh-curve prime256v1
data-ciphers AES-256-GCM:AES-128-GCM
data-ciphers-fallback AES-256-CBC
sndbuf 0
rcvbuf 0
tun-mtu 1400
verb 3
status /var/log/openvpn-status.log
+4
View File
@@ -0,0 +1,4 @@
---
# defaults file for roles/networking
radius_auth_nodeport: 30131
radius_acct_nodeport: 31622
+27
View File
@@ -0,0 +1,27 @@
---
- name: Install iptables persistence
apt:
name:
- iptables-persistent
state: present
- name: DNAT RADIUS auth traffic
iptables:
table: nat
chain: OUTPUT
protocol: udp
destination_port: "1812"
jump: DNAT
to_destination: ":{{ radius_auth_nodeport }}"
- name: DNAT RADIUS accounting traffic
iptables:
table: nat
chain: OUTPUT
protocol: udp
destination_port: "1813"
jump: DNAT
to_destination: ":{{ radius_acct_nodeport }}"
- name: Save iptables rules
command: netfilter-persistent save