vm-ansible/roles/openvpn/tasks/main.yaml

65 lines
1.7 KiB
YAML
Raw Normal View History

2022-04-14 16:10:43 +02:00
---
- name: "Install openvpn-server via apt"
2023-08-30 23:13:59 +02:00
apt:
2022-04-14 16:10:43 +02:00
update_cache: yes
state: present
name:
2023-02-19 13:42:53 +01:00
- openvpn
2022-04-14 16:10:43 +02:00
2022-04-14 21:41:04 +02:00
- name : "Enable ipv4 forwarding via sysctl"
ansible.posix.sysctl:
name: net.ipv4.ip_forward
value: '1'
sysctl_set: yes
state: present
reload: yes
2022-04-14 16:10:43 +02:00
- name: Enable and restart openvpn daemon
2023-08-30 23:13:59 +02:00
service:
2023-02-19 13:42:53 +01:00
name: openvpn-server@stargate
2022-04-14 16:10:43 +02:00
state: restarted
enabled: yes
2022-04-17 12:22:22 +02:00
- name: Check if AllowUsers is defined
2023-08-30 23:13:59 +02:00
lineinfile:
2022-04-17 12:22:22 +02:00
state: absent
path: /etc/ufw/before.rules
regexp: "^# START OPENVPN"
check_mode: true
changed_when: false
register: checkufwrules
- name: Insert openvpn iptables rules
2023-08-30 23:13:59 +02:00
blockinfile:
2022-04-17 12:22:22 +02:00
path: /etc/ufw/before.rules
block: |
# START OPENVPN RULES
# NAT table rules
*nat
2023-03-05 22:02:29 +01:00
-F
2022-04-17 12:22:22 +02:00
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to everywhere
-A POSTROUTING -s 192.168.37.0/24 -o eth0 -j MASQUERADE
-A POSTROUTING -s 192.168.37.0/24 -o eth2 -j MASQUERADE
COMMIT
2023-03-05 22:02:29 +01:00
*filter
2023-02-19 13:42:53 +01:00
-A ufw-before-input -i tun+ -j ACCEPT
-A ufw-before-forward -i tun+ -j ACCEPT
-A ufw-before-forward -i tun+ -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-forward -i eth0 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-forward -i tun+ -o eth2 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-forward -i eth2 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
2022-04-17 12:22:22 +02:00
# END OPENVPN RULES
- name: Reload ufw
community.general.ufw:
state: reloaded
2023-03-05 21:53:06 +01:00
- name: Apply custom ufw rules
community.general.ufw:
rule: allow
direction: "in"
2023-03-05 22:04:46 +01:00
interface: tun0
2022-04-16 19:55:29 +02:00
...