Ansible VPN Automation: Playbooks, Modules & Verification

Ansible VPN Automation: Playbooks, Modules, and Verification

Ansible VPN automation means using agentless, YAML-based playbooks and modules to provision VPN servers, configure IPsec tunnels, and manage cloud VPN connections at scale. Instead of clicking through a web console or hand-editing config files on each host, you describe the desired state once and let Ansible converge every target to it. This guide covers the three main ways Ansible handles VPN work, how to structure a playbook, and how to verify the tunnel actually protects your traffic after deployment.

What "Ansible VPN" Actually Means

Search results for this term fall into three distinct categories, and knowing which one you need saves time:

  • Self-hosted VPN server setup — a community playbook that installs and configures an OpenVPN (or WireGuard) server on a Linux host. This is the most common interpretation for homelab and small-team use.
  • Vendor appliance configuration — a collection module such as fortinet.fortios.fortios_vpn_ipsec_phase1_interface, which manages VPN remote gateway settings on FortiGate devices through FortiOS.
  • Cloud VPN connections — a module such as amazon.aws.ec2_vpc_vpn, which creates, modifies, and deletes AWS Site-to-Site VPN connections between a virtual private gateway and a customer gateway.

All three share the same Ansible model: declarative tasks, idempotent execution, and inventory-driven targeting. The difference is whether Ansible is configuring an operating system, a network appliance, or a cloud API.

Option 1: A Community Playbook for a Self-Hosted VPN Server

The simplest starting point is a ready-made playbook repository, such as the javascriptizer1/ansible-vpn project, which automates OpenVPN server setup. A typical workflow looks like this:

  1. Install Ansible on your control machine.
  2. Clone the playbook repository and change into its directory.
  3. Edit inventory.ini to list your target hosts.
  4. Adjust defaults/main.yaml for VPN server type, port, DNS settings, and client options.
  5. Run the playbook against a limited host group.
ansible-playbook -i inventories/inventory.ini -l vpn playbook.yaml --diff -v

The -l vpn flag limits execution to the vpn group, and --diff shows exactly which files changed. That combination is worth keeping in your muscle memory: it prevents a stray run from reconfiguring unrelated hosts.

What to Customize Before You Run It

Setting Why it matters
VPN server type OpenVPN and WireGuard have different client tooling and performance profiles
Server port Non-default ports reduce noise from automated scanners
DNS server A misconfigured DNS server is the most common cause of leaks
Client settings Determines whether all traffic is tunneled or only specific subnets
Hardening options Firewall rules, SSH restrictions, and service exposure

Treat the playbook as a starting template, not a finished product. Read every task before running it against a production host.

Option 2: Managing FortiGate IPsec with the FortiOS Collection

For organizations running Fortinet hardware, the fortinet.fortios collection exposes VPN configuration as Ansible tasks. The fortios_vpn_ipsec_phase1_interface module configures the VPN remote gateway — the phase 1 parameters of an IPsec tunnel.

Key parameters you will encounter include:

  • state — present to create, absent to remove.
  • vdom — the virtual domain, defaulting to root.
  • authmethod — psk for pre-shared key or signature for certificate authentication.
  • dpd — Dead Peer Detection mode (disable, on-idle, or on-demand).
  • dhgrp — Diffie-Hellman group selection for key exchange.
  • add_gw_route and add_route — control automatic route insertion.

A minimal task looks roughly like this:

- name: Configure IPsec phase 1
  fortinet.fortios.fortios_vpn_ipsec_phase1_interface:
    access_token: "{{ fortigate_token }}"
    state: present
    vpn_ipsec_phase1_interface:
      name: "to-branch-office"
      interface: "wan1"
      authmethod: psk
      dpd: on-idle

Because the module is idempotent, re-running the playbook will not duplicate the tunnel — it will only apply differences. Store the access_token in Ansible Vault rather than in plaintext inventory files.

Option 3: Provisioning AWS Site-to-Site VPN with ec2_vpc_vpn

The amazon.aws.ec2_vpc_vpn module creates, modifies, and deletes EC2 VPN connections. It requires Python 3.6 or later plus boto3 and botocore.

Notable behaviors:

  • Idempotence is achieved through the filters option or by specifying vpn_connection_id directly.
  • connection_type currently supports only ipsec.1.
  • tunnel_options accepts up to two dictionaries, each carrying TunnelInsideCidr, TunnelInsideIpv6Cidr, and PreSharedKey.
  • static_only: true is required for customer gateways that do not support BGP.
  • purge_routes and purge_tags control whether unspecified routes and tags are removed.
- name: Ensure VPN connection exists
  amazon.aws.ec2_vpc_vpn:
    state: present
    region: us-east-1
    vpn_gateway_id: vgw-0123456789abcdef0
    customer_gateway_id: cgw-0123456789abcdef0
    static_only: true
    routes:
      - 10.0.0.0/16
    tags:
      Name: branch-vpn

If multiple VPNs match your filters, the module requires an explicit vpn_connection_id. Ambiguous filters are a common source of failed runs.

Choosing the Right Approach

Scenario Best fit
Small team, self-managed Linux server Community OpenVPN or WireGuard playbook
Existing FortiGate estate fortinet.fortios VPN modules
AWS hybrid connectivity amazon.aws.ec2_vpc_vpn
Mixed environment Separate roles per platform, shared inventory

Do not try to force one playbook to cover all three. Split them into roles and let a top-level playbook include the ones relevant to each host group.

Verifying the Tunnel After Deployment

Automation gets the tunnel up. It does not prove the tunnel is protecting your traffic. A VPN can connect successfully while still leaking your real IP address through DNS queries or WebRTC.

After any Ansible run, verify from a client machine:

  1. Check your visible IP — confirm the address shown matches the VPN exit, not your ISP. An IP address lookup reveals geolocation, ISP, and proxy detection signals.
  2. Run a DNS leak test — confirm DNS queries resolve through the tunnel's resolver, not your local network's.
  3. Run a WebRTC leak test — browsers can expose local and public IPs through WebRTC even when a VPN is active.
  4. Check browser fingerprint consistency — if your IP says one country and your fingerprint says another, that mismatch is itself a signal.

BrowserScan provides free, browser-based versions of these checks — DNS leak testing, WebRTC leak testing, IP lookup, and fingerprint analysis — with no installation required. It is a practical post-deployment step for anyone who just automated a VPN and wants evidence that it works.

If you are running a self-hosted server, pair the VPN check with a port scan of the host. Ansible playbooks frequently leave services listening on interfaces you did not intend to expose.

Common Pitfalls

  • Hardcoding secrets. Use Ansible Vault for pre-shared keys, tokens, and credentials.
  • Skipping --check. A dry run catches destructive changes before they happen.
  • Ignoring DNS configuration. The tunnel may be up while DNS still resolves locally.
  • Assuming idempotence. Modules are idempotent; hand-written shell and command tasks are not.
  • Never verifying. Deployment success and privacy success are two different outcomes.

Related reading

Sources and further reading

FAQ

Is Ansible a VPN?

No. Ansible is a configuration management and automation tool. It can deploy and configure VPN software, but it is not itself a VPN protocol or service.

Can Ansible set up WireGuard?

Yes. WireGuard configuration is file-based and well suited to Ansible. Many community roles and playbooks exist, and the same inventory-driven pattern applies.

What is the difference between fortios_vpn_ipsec_phase1_interface and ec2_vpc_vpn?

The first configures IPsec phase 1 parameters on a FortiGate appliance. The second manages AWS Site-to-Site VPN connections through the AWS API. They solve different problems on different platforms.

How do I verify my VPN is not leaking after an Ansible deployment?

Run a DNS leak test, a WebRTC leak test, and an IP lookup from a client device. Compare the results against the VPN exit you configured. BrowserScan offers all three as free browser-based tools.

Does Ansible work with OpenVPN?

Yes. OpenVPN server installation, certificate generation, and client configuration can all be expressed as Ansible tasks, which is exactly what community playbooks like the one above do.

Conclusion

Ansible turns VPN configuration from a manual, error-prone process into a repeatable, version-controlled one. Whether you are deploying OpenVPN on a single VPS, managing IPsec gateways across a FortiGate fleet, or wiring AWS Site-to-Site connections, the pattern is the same: declare the desired state, target the right hosts, and run the playbook.

The step most teams skip is verification. A tunnel that reports "connected" can still leak DNS queries or expose your real IP through WebRTC. After every deployment, confirm what your network actually reveals — then fix the gap before it becomes an incident.