feat(ansible): automate NAT gateway and VPN routing
OpenVPN Ansible / Ansible Syntax Check (push) Successful in 10s
OpenVPN Ansible / Ansible Lint (push) Successful in 17s
OpenVPN Ansible / Deploy VPN Networking and OpenVPN (push) Failing after 20s
OpenVPN Ansible / Generate SBOM (push) Skipped
OpenVPN Ansible / Scan SBOM with Trivy (push) Skipped
OpenVPN Ansible / Ansible Syntax Check (push) Successful in 10s
OpenVPN Ansible / Ansible Lint (push) Successful in 17s
OpenVPN Ansible / Deploy VPN Networking and OpenVPN (push) Failing after 20s
OpenVPN Ansible / Generate SBOM (push) Skipped
OpenVPN Ansible / Scan SBOM with Trivy (push) Skipped
- Add NAT gateway inventory and routing playbooks. - Update Gitea Actions and GitLab CI to validate all playbooks. - Deploy gateway and routing configurations before OpenVPN. - Document setup and verification steps.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
nat_gateway_private_cidr: 10.100.0.0/16
|
||||
nat_gateway_private_interface: enp7s0
|
||||
nat_gateway_public_interface: eth0
|
||||
nat_gateway_firewall_script: /usr/local/sbin/ensure-nat-gateway-firewall
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Apply NAT gateway sysctl
|
||||
ansible.builtin.command: sysctl --system
|
||||
changed_when: false
|
||||
- name: Restart NAT gateway firewall persistence
|
||||
ansible.builtin.systemd:
|
||||
name: nat-gateway-firewall.service
|
||||
enabled: true
|
||||
daemon_reload: true
|
||||
state: restarted
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
- name: Install iptables tools
|
||||
ansible.builtin.apt:
|
||||
name: iptables
|
||||
state: present
|
||||
|
||||
- name: Install idempotent NAT firewall restore script
|
||||
ansible.builtin.template:
|
||||
src: ensure-nat-gateway-firewall.j2
|
||||
dest: "{{ nat_gateway_firewall_script }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0750"
|
||||
notify: Restart NAT gateway firewall persistence
|
||||
|
||||
- name: Install NAT firewall persistence unit
|
||||
ansible.builtin.template:
|
||||
src: nat-gateway-firewall.service.j2
|
||||
dest: /etc/systemd/system/nat-gateway-firewall.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: Restart NAT gateway firewall persistence
|
||||
|
||||
- name: Enable NAT firewall persistence at boot
|
||||
ansible.builtin.systemd:
|
||||
name: nat-gateway-firewall.service
|
||||
enabled: true
|
||||
daemon_reload: true
|
||||
state: started
|
||||
|
||||
- name: Configure persistent IPv4 forwarding
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/sysctl.d/99-nat-gateway.conf
|
||||
content: "net.ipv4.ip_forward=1\n"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: Apply NAT gateway sysctl
|
||||
|
||||
- name: Enable IPv4 forwarding immediately
|
||||
ansible.posix.sysctl:
|
||||
name: net.ipv4.ip_forward
|
||||
value: "1"
|
||||
sysctl_file: /etc/sysctl.d/99-nat-gateway.conf
|
||||
reload: true
|
||||
|
||||
- name: Allow private subnet outbound through public interface
|
||||
ansible.builtin.iptables:
|
||||
table: nat
|
||||
chain: POSTROUTING
|
||||
source: "{{ nat_gateway_private_cidr }}"
|
||||
out_interface: "{{ nat_gateway_public_interface }}"
|
||||
jump: MASQUERADE
|
||||
state: present
|
||||
notify: Restart NAT gateway firewall persistence
|
||||
|
||||
- name: Allow private subnet forwarding to public interface
|
||||
ansible.builtin.iptables:
|
||||
chain: FORWARD
|
||||
in_interface: "{{ nat_gateway_private_interface }}"
|
||||
out_interface: "{{ nat_gateway_public_interface }}"
|
||||
source: "{{ nat_gateway_private_cidr }}"
|
||||
jump: ACCEPT
|
||||
state: present
|
||||
notify: Restart NAT gateway firewall persistence
|
||||
|
||||
- name: Allow established return traffic to private subnet
|
||||
ansible.builtin.iptables:
|
||||
chain: FORWARD
|
||||
in_interface: "{{ nat_gateway_public_interface }}"
|
||||
out_interface: "{{ nat_gateway_private_interface }}"
|
||||
destination: "{{ nat_gateway_private_cidr }}"
|
||||
ctstate: [ESTABLISHED, RELATED]
|
||||
jump: ACCEPT
|
||||
state: present
|
||||
notify: Restart NAT gateway firewall persistence
|
||||
|
||||
- name: Assert gateway retains public default route
|
||||
ansible.builtin.command: ip -4 route show default
|
||||
register: nat_gateway_default_route
|
||||
changed_when: false
|
||||
failed_when: nat_gateway_public_interface not in nat_gateway_default_route.stdout
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
iptables -t nat -C POSTROUTING -s {{ nat_gateway_private_cidr }} -o {{ nat_gateway_public_interface }} -j MASQUERADE 2>/dev/null || \
|
||||
iptables -t nat -A POSTROUTING -s {{ nat_gateway_private_cidr }} -o {{ nat_gateway_public_interface }} -j MASQUERADE
|
||||
|
||||
iptables -C FORWARD -i {{ nat_gateway_private_interface }} -o {{ nat_gateway_public_interface }} -s {{ nat_gateway_private_cidr }} -j ACCEPT 2>/dev/null || \
|
||||
iptables -A FORWARD -i {{ nat_gateway_private_interface }} -o {{ nat_gateway_public_interface }} -s {{ nat_gateway_private_cidr }} -j ACCEPT
|
||||
|
||||
iptables -C FORWARD -i {{ nat_gateway_public_interface }} -o {{ nat_gateway_private_interface }} -d {{ nat_gateway_private_cidr }} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT 2>/dev/null || \
|
||||
iptables -A FORWARD -i {{ nat_gateway_public_interface }} -o {{ nat_gateway_private_interface }} -d {{ nat_gateway_private_cidr }} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
|
||||
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Ensure scoped NAT gateway firewall rules are present
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart={{ nat_gateway_firewall_script }}
|
||||
RemainAfterExit=yes
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,4 @@
|
||||
---
|
||||
private_routing_interface: enp7s0
|
||||
private_routing_gateway: 10.0.0.1
|
||||
private_routing_metric: 100
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Apply private VPN route
|
||||
ansible.builtin.command: netplan apply
|
||||
changed_when: false
|
||||
when: private_routing_netplan_files.matched | default(0) > 0
|
||||
- name: Reload systemd-networkd routes
|
||||
ansible.builtin.systemd:
|
||||
name: systemd-networkd
|
||||
state: restarted
|
||||
when: private_routing_netplan_files.matched | default(0) == 0
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
- name: Find Netplan configuration files
|
||||
ansible.builtin.find:
|
||||
paths: /etc/netplan
|
||||
patterns: "*.yaml,*.yml"
|
||||
file_type: file
|
||||
register: private_routing_netplan_files
|
||||
failed_when: false
|
||||
|
||||
- name: Determine whether systemd-networkd manages the interface
|
||||
ansible.builtin.command: networkctl status "{{ private_routing_interface }}"
|
||||
register: private_routing_networkctl
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
when: private_routing_netplan_files.matched | default(0) == 0
|
||||
|
||||
- name: Refuse unknown network manager rather than overwrite network settings
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (private_routing_netplan_files.matched | default(0) > 0) or
|
||||
(private_routing_networkctl.rc | default(1) == 0)
|
||||
fail_msg: >-
|
||||
No Netplan configuration or active systemd-networkd interface found for
|
||||
{{ private_routing_interface }}; refusing to change network configuration.
|
||||
|
||||
- name: Install Netplan route drop-in
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/netplan/99-private-vpn-default-route.yaml
|
||||
content: |
|
||||
network:
|
||||
version: 2
|
||||
ethernets:
|
||||
{{ private_routing_interface }}:
|
||||
routes:
|
||||
- to: 0.0.0.0/0
|
||||
via: {{ private_routing_gateway }}
|
||||
metric: {{ private_routing_metric }}
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
validate: netplan generate --debug
|
||||
when: private_routing_netplan_files.matched | default(0) > 0
|
||||
notify: Apply private VPN route
|
||||
|
||||
- name: Install systemd-networkd route drop-in
|
||||
ansible.builtin.copy:
|
||||
dest: "/etc/systemd/network/90-{{ private_routing_interface }}-private-vpn-route.network"
|
||||
content: |
|
||||
[Match]
|
||||
Name={{ private_routing_interface }}
|
||||
|
||||
[Route]
|
||||
Destination=0.0.0.0/0
|
||||
Gateway={{ private_routing_gateway }}
|
||||
Metric={{ private_routing_metric }}
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
when: private_routing_netplan_files.matched | default(0) == 0
|
||||
notify: Apply private VPN route
|
||||
|
||||
- name: Verify selected route to internet
|
||||
ansible.builtin.command: ip -4 route get 8.8.8.8
|
||||
register: private_route_get
|
||||
changed_when: false
|
||||
failed_when: private_routing_gateway not in private_route_get.stdout or private_routing_interface not in private_route_get.stdout
|
||||
Reference in New Issue
Block a user