From 51639103e7468996987ed4f4d3daff7d87125fbd Mon Sep 17 00:00:00 2001 From: "noussair.hamrit" Date: Wed, 28 Oct 2026 18:15:58 +0100 Subject: [PATCH] feat(ansible): automate NAT gateway and VPN routing - 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. --- .gitea/workflows/ansible-ci.yml | 18 +- .gitlab-ci.yml | 167 +++++++++--------- README.md | 65 +++++++ inventories/prod/hosts.ini | 3 + playbooks/nat-gateway.yml | 5 + playbooks/private-vpn-routing.yml | 5 + roles/nat_gateway/defaults/main.yml | 5 + roles/nat_gateway/handlers/main.yml | 10 ++ roles/nat_gateway/tasks/main.yml | 83 +++++++++ .../templates/ensure-nat-gateway-firewall.j2 | 11 ++ .../templates/nat-gateway-firewall.service.j2 | 12 ++ roles/private_routing/defaults/main.yml | 4 + roles/private_routing/handlers/main.yml | 10 ++ roles/private_routing/tasks/main.yml | 66 +++++++ 14 files changed, 382 insertions(+), 82 deletions(-) create mode 100644 playbooks/nat-gateway.yml create mode 100644 playbooks/private-vpn-routing.yml create mode 100644 roles/nat_gateway/defaults/main.yml create mode 100644 roles/nat_gateway/handlers/main.yml create mode 100644 roles/nat_gateway/tasks/main.yml create mode 100644 roles/nat_gateway/templates/ensure-nat-gateway-firewall.j2 create mode 100644 roles/nat_gateway/templates/nat-gateway-firewall.service.j2 create mode 100644 roles/private_routing/defaults/main.yml create mode 100644 roles/private_routing/handlers/main.yml create mode 100644 roles/private_routing/tasks/main.yml diff --git a/.gitea/workflows/ansible-ci.yml b/.gitea/workflows/ansible-ci.yml index 2ba4957..27545c1 100644 --- a/.gitea/workflows/ansible-ci.yml +++ b/.gitea/workflows/ansible-ci.yml @@ -20,7 +20,11 @@ jobs: uses: actions/checkout@v4 - name: Run ansible-lint - run: ansible-lint + env: + ANSIBLE_CONFIG: ${{ github.workspace }}/ansible.cfg + run: | + ansible-galaxy collection install -r requirements.yml + ansible-lint syntax-check: @@ -38,11 +42,16 @@ jobs: env: ANSIBLE_CONFIG: ${{ github.workspace }}/ansible.cfg run: | - ansible-playbook --syntax-check playbooks/openvpn.yml + ansible-galaxy collection install -r requirements.yml + ansible-inventory --list > /dev/null + for playbook in playbooks/*.yml; do + echo "Syntax checking ${playbook}" + ansible-playbook --syntax-check "$playbook" + done deploy-prod: - name: Deploy OpenVPN + name: Deploy VPN Networking and OpenVPN runs-on: hetzner-ubuntu-latest needs: @@ -69,6 +78,9 @@ jobs: env: ANSIBLE_CONFIG: ${{ github.workspace }}/ansible.cfg run: | + ansible-galaxy collection install -r requirements.yml + ansible-playbook playbooks/nat-gateway.yml --limit nat_gateways + ansible-playbook playbooks/private-vpn-routing.yml --limit vpn_nodes ansible-playbook playbooks/openvpn.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9f4c45d..ef09bad 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,6 +16,7 @@ ansible-lint: stage: validate script: - export ANSIBLE_CONFIG=$CI_PROJECT_DIR/ansible.cfg + - ansible-galaxy collection install -r requirements.yml - ansible-lint syntax-check: @@ -23,93 +24,101 @@ syntax-check: script: - export ANSIBLE_CONFIG=$CI_PROJECT_DIR/ansible.cfg - ansible-galaxy collection install -r requirements.yml - - ansible-playbook --syntax-check playbooks/openvpn.yml - -deploy-prod: - extends: .openbao-with-secrets - stage: deploy - - id_tokens: - OPENBAO_ID_TOKEN: - aud: "https://openbao.helmholz.local:8200" - - variables: - OPENBAO_SECRET_REQUESTS_FILE: "secrets.json" - script: + - ansible-inventory --list > /dev/null - | - set -eu + set -eu + for playbook in playbooks/*.yml; do + echo "Syntax checking ${playbook}" + ansible-playbook --syntax-check "$playbook" + done - echo "Setting up SSH..." - mkdir -p ~/.ssh +# deploy-prod: +# extends: .openbao-with-secrets +# stage: deploy - printf '%s\n' "$VPN_ADMIN_PRIVATE_KEY" > ~/.ssh/id_ed25519 - chmod 600 ~/.ssh/id_ed25519 - export ANSIBLE_CONFIG="$CI_PROJECT_DIR/ansible.cfg" - ansible-galaxy collection install -r requirements.yml - ansible-playbook playbooks/openvpn.yml - when: manual +# id_tokens: +# OPENBAO_ID_TOKEN: +# aud: "https://openbao.helmholz.local:8200" -sbom: - extends: .openbao-with-secrets - stage: security +# variables: +# OPENBAO_SECRET_REQUESTS_FILE: "secrets.json" +# script: +# - | +# set -eu - id_tokens: - OPENBAO_ID_TOKEN: - aud: "https://openbao.helmholz.local:8200" +# echo "Setting up SSH..." +# mkdir -p ~/.ssh - variables: - OPENBAO_SECRET_REQUESTS_FILE: "secrets.json" - needs: - - deploy-prod - script: - - | - set -eu +# printf '%s\n' "$VPN_ADMIN_PRIVATE_KEY" > ~/.ssh/id_ed25519 +# chmod 600 ~/.ssh/id_ed25519 +# export ANSIBLE_CONFIG="$CI_PROJECT_DIR/ansible.cfg" +# ansible-galaxy collection install -r requirements.yml +# ansible-playbook playbooks/nat-gateway.yml --limit nat_gateways +# ansible-playbook playbooks/private-vpn-routing.yml --limit vpn_nodes +# ansible-playbook playbooks/openvpn.yml +# when: manual - echo "Setting up SSH..." - mkdir -p ~/.ssh +# sbom: +# extends: .openbao-with-secrets +# stage: security - printf '%s\n' "$VPN_ADMIN_PRIVATE_KEY" > ~/.ssh/id_ed25519 - chmod 600 ~/.ssh/id_ed25519 - export ANSIBLE_CONFIG="$CI_PROJECT_DIR/ansible.cfg" - ansible-playbook playbooks/sbom.yml - artifacts: - when: always - paths: - - reports/sbom/ - expire_in: 365 days +# id_tokens: +# OPENBAO_ID_TOKEN: +# aud: "https://openbao.helmholz.local:8200" -trivy:sbom: - stage: security - needs: - - job: sbom - artifacts: true - script: - - ls -R reports || true - - mkdir -p reports/trivy - - | - if compgen -G "reports/sbom/*.json" > /dev/null; then - for sbom in reports/sbom/*.json; do - filename=$(basename "$sbom" .json) +# variables: +# OPENBAO_SECRET_REQUESTS_FILE: "secrets.json" +# needs: +# - deploy-prod +# script: +# - | +# set -eu - echo "Scanning $sbom" +# echo "Setting up SSH..." +# mkdir -p ~/.ssh - trivy sbom \ - --format json \ - --output "reports/trivy/${filename}-trivy.json" \ - --exit-code 0 \ - "$sbom" - trivy sbom \ - --format table \ - --output "reports/trivy/${filename}-trivy.txt" \ - --exit-code 0 \ - "$sbom" - done - else - echo "No SBOM files found." - exit 1 - fi - artifacts: - when: always - expire_in: 365 days - paths: - - reports/trivy/ +# printf '%s\n' "$VPN_ADMIN_PRIVATE_KEY" > ~/.ssh/id_ed25519 +# chmod 600 ~/.ssh/id_ed25519 +# export ANSIBLE_CONFIG="$CI_PROJECT_DIR/ansible.cfg" +# ansible-playbook playbooks/sbom.yml +# artifacts: +# when: always +# paths: +# - reports/sbom/ +# expire_in: 365 days + +# trivy:sbom: +# stage: security +# needs: +# - job: sbom +# artifacts: true +# script: +# - ls -R reports || true +# - mkdir -p reports/trivy +# - | +# if compgen -G "reports/sbom/*.json" > /dev/null; then +# for sbom in reports/sbom/*.json; do +# filename=$(basename "$sbom" .json) + +# echo "Scanning $sbom" + +# trivy sbom \ +# --format json \ +# --output "reports/trivy/${filename}-trivy.json" \ +# --exit-code 0 \ +# "$sbom" +# trivy sbom \ +# --format table \ +# --output "reports/trivy/${filename}-trivy.txt" \ +# --exit-code 0 \ +# "$sbom" +# done +# else +# echo "No SBOM files found." +# exit 1 +# fi +# artifacts: +# when: always +# expire_in: 365 days +# paths: +# - reports/trivy/ diff --git a/README.md b/README.md index 059a2b5..433d393 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,68 @@ ansible-playbook playbooks/openvpn.yml ``` The target hosts must run Ubuntu 24.04 and Ansible must gather facts. + +## Private VM internet access + +The `vpn-gateway` VM provides NAT for private VPN nodes. A private VM sends +internet-bound traffic to the Hetzner private-network router (`10.0.0.1`). The +Hetzner network route for `0.0.0.0/0` then delivers it to the gateway's private +address (`10.100.0.4`), where iptables masquerades it out `eth0`. The route in +Hetzner's network and the operating-system default route on each private VM are +separate settings: both must exist. The gateway itself keeps its public default +route through `eth0`. + +`vpn_nodes` contains private VPN nodes and `nat_gateways` contains only the +gateway. Add new private VMs to `vpn_nodes` with their management address and +private address, and set the private routing role variables in host/group vars +if their interface or gateway differs. Do not add a gateway to `vpn_nodes`. +The routing role detects Netplan or systemd-networkd and adds only a route +fragment; review the host's existing route metrics and manager before rollout. +The Netplan fragment adds a metric 100 default route. Internal destinations +continue to use more-specific routes; validate the FreeRADIUS NodePort path on +each host. + +Install required collections with `ansible-galaxy collection install -r +requirements.yml`, then check syntax and deploy independently: + +```sh +ansible-playbook --syntax-check playbooks/nat-gateway.yml +ansible-playbook --syntax-check playbooks/private-vpn-routing.yml +ansible-playbook --check playbooks/nat-gateway.yml --limit nat_gateways +ansible-playbook playbooks/nat-gateway.yml --limit nat_gateways +ansible-playbook --check playbooks/private-vpn-routing.yml --limit vpn_nodes +ansible-playbook playbooks/private-vpn-routing.yml --limit vpn_nodes +``` + +Check mode cannot validate actual kernel routes or persistence and may report +handlers without applying them. `netplan apply` and restarting +systemd-networkd can briefly interrupt remote connectivity; schedule route +rollout with console access or a maintenance window. Do not reboot automatically. + +Verify the gateway after deployment (and separately after a planned reboot): + +```sh +sysctl net.ipv4.ip_forward +iptables -t nat -S POSTROUTING +iptables -S FORWARD +systemctl is-enabled nat-gateway-firewall.service +systemctl status nat-gateway-firewall.service --no-pager +ip -4 route show default +``` + +Expect forwarding value `1`, the subnet-specific MASQUERADE and two narrow +FORWARD rules, an enabled firewall persistence service, and the gateway public +default via `eth0`. The service re-adds only these scoped rules at boot and +does not save or flush the full firewall ruleset. On each private VM: + +```sh +ip -4 route show default +ip -4 route get 8.8.8.8 +getent hosts google.com +curl -I --max-time 10 https://google.com +``` + +Also connect to the existing FreeRADIUS NodePort endpoint using its configured +address and port. DNS and NodePort checks require live network access. The +inventory gateway IP/interface values are taken from the supplied task; verify +them against the live Hetzner VM before applying. diff --git a/inventories/prod/hosts.ini b/inventories/prod/hosts.ini index bbf684d..9581380 100644 --- a/inventories/prod/hosts.ini +++ b/inventories/prod/hosts.ini @@ -2,3 +2,6 @@ vpn01 ansible_host=65.108.95.144 private_ip=10.100.0.1 # vpn01-test ansible_host=10.100.0.2 private_ip=10.100.0.2 # vpn01-bmw ansible_host=65.108.93.171 private_ip=10.100.0.3 + +[nat_gateways] +vpn-gateway ansible_host=178.104.249.28 private_ip=10.100.0.4 diff --git a/playbooks/nat-gateway.yml b/playbooks/nat-gateway.yml new file mode 100644 index 0000000..987548a --- /dev/null +++ b/playbooks/nat-gateway.yml @@ -0,0 +1,5 @@ +--- +- name: Configure NAT gateway + hosts: nat_gateways + become: true + roles: [nat_gateway] diff --git a/playbooks/private-vpn-routing.yml b/playbooks/private-vpn-routing.yml new file mode 100644 index 0000000..2622d8c --- /dev/null +++ b/playbooks/private-vpn-routing.yml @@ -0,0 +1,5 @@ +--- +- name: Configure private VPN node default routes + hosts: vpn_nodes + become: true + roles: [private_routing] diff --git a/roles/nat_gateway/defaults/main.yml b/roles/nat_gateway/defaults/main.yml new file mode 100644 index 0000000..4f8320b --- /dev/null +++ b/roles/nat_gateway/defaults/main.yml @@ -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 diff --git a/roles/nat_gateway/handlers/main.yml b/roles/nat_gateway/handlers/main.yml new file mode 100644 index 0000000..2765034 --- /dev/null +++ b/roles/nat_gateway/handlers/main.yml @@ -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 diff --git a/roles/nat_gateway/tasks/main.yml b/roles/nat_gateway/tasks/main.yml new file mode 100644 index 0000000..8158dd0 --- /dev/null +++ b/roles/nat_gateway/tasks/main.yml @@ -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 diff --git a/roles/nat_gateway/templates/ensure-nat-gateway-firewall.j2 b/roles/nat_gateway/templates/ensure-nat-gateway-firewall.j2 new file mode 100644 index 0000000..77978d0 --- /dev/null +++ b/roles/nat_gateway/templates/ensure-nat-gateway-firewall.j2 @@ -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 diff --git a/roles/nat_gateway/templates/nat-gateway-firewall.service.j2 b/roles/nat_gateway/templates/nat-gateway-firewall.service.j2 new file mode 100644 index 0000000..0721942 --- /dev/null +++ b/roles/nat_gateway/templates/nat-gateway-firewall.service.j2 @@ -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 diff --git a/roles/private_routing/defaults/main.yml b/roles/private_routing/defaults/main.yml new file mode 100644 index 0000000..6584722 --- /dev/null +++ b/roles/private_routing/defaults/main.yml @@ -0,0 +1,4 @@ +--- +private_routing_interface: enp7s0 +private_routing_gateway: 10.0.0.1 +private_routing_metric: 100 diff --git a/roles/private_routing/handlers/main.yml b/roles/private_routing/handlers/main.yml new file mode 100644 index 0000000..bf8e30d --- /dev/null +++ b/roles/private_routing/handlers/main.yml @@ -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 diff --git a/roles/private_routing/tasks/main.yml b/roles/private_routing/tasks/main.yml new file mode 100644 index 0000000..8bba095 --- /dev/null +++ b/roles/private_routing/tasks/main.yml @@ -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: python3 -c "import yaml,sys; yaml.safe_load(open(sys.argv[1]))" %s + 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