Back to articles
Technology Insight

Automating Pentest Lab Deployments: A One-Click OpenTofu and Ansible Framework on VPS

June 4, 2026

Introduction: The Imperative for Automated Security Environments

In the rapidly evolving landscape of cybersecurity, professionals, researchers, and enterprise security teams require agile, isolated, and highly reproducible environments to conduct vulnerability assessments, malware analysis, and penetration testing. Traditionally, setting up a comprehensive pentest lab involved manual provisioning of Virtual Private Servers (VPS), tedious network configurations, and hours of software installation. This manual approach not only consumes valuable engineering hours but also introduces configuration drift and human error, undermining the consistency required for rigorous security testing.

To mitigate these challenges, modern security operations are increasingly adopting the principles of Infrastructure as Code (IaC) and Configuration as Code (CaC). By leveraging enterprise-grade automation tools, teams can transition from manual setups to a paradigm where an entire multi-node pentest lab can be deployed with a single command. This article provides an architectural blueprint for building a 'One-Click Deploy' pentest lab environment on a cloud VPS using OpenTofu for immutable infrastructure provisioning and Ansible for dynamic configuration management.

---

The Architectural Blueprint: OpenTofu and Ansible Combined

Achieving a reliable 'One-Click' deployment model requires a decoupled architecture where provisioning and configuration are handled by distinct, specialized tools. Attempting to manage both layers with a single tool often leads to fragile scripts and unmaintainable codebases.

1. OpenTofu: The Infrastructure Foundation

OpenTofu, an open-source successor to Terraform managed by the Linux Foundation, serves as our declarative infrastructure engine. It interacts directly with your VPS provider's API (such as DigitalOcean, AWS, Linode, or Vultr) to provision core resources. OpenTofu ensures that the underlying computing instances, networking topologies, firewalls, and storage blocks are created exactly as specified in the configuration files. Its state management capabilities ensure that any subsequent modifications to the infrastructure are executed predictably without destroying existing, intact components.

2. Ansible: The Configuration Engine

Once OpenTofu establishes the hardware and networking foundation, Ansible takes over to handle software delivery and operating system orchestration. As an agentless automation tool, Ansible connects to the newly provisioned VPS nodes securely via SSH. It executes structured Playbooks to update packages, configure secure network tunnels, deploy vulnerable target containers, and provision specialized attacking tools (such as Kali Linux utilities or customized Command and Control frameworks). Ansible's idempotent nature guarantees that running the playbook multiple times yields the same state without adverse side effects.

---

Step-by-Step Implementation Guide

To implement this automation framework, the deployment lifecycle is divided into three core phases: infrastructure provisioning, host handoff, and configuration orchestration.

Phase 1: Provisioning the VPS with OpenTofu

The first step involves defining our target cloud infrastructure. Below is a structured approach to setting up the OpenTofu configuration files. The deployment typically requires at least two distinct zones within the VPS network: an attacker node and a vulnerable target subnet.

  • provider.tf: Configures the connection to your chosen cloud vendor API and defines required authentication tokens.
  • main.tf: Specifies the compute resources. For a robust pentest lab, we provision one high-performance instance for the attacking platform and a separate, isolated instance to host vulnerable targets (e.g., OWASP Juice Shop, Metasploitable, or custom Active Directory targets).
  • firewall.tf: Restricts all inbound traffic exclusively to the operator's public IP address while allowing unrestricted internal communication between the attacker and target nodes.
Security Note: Never expose vulnerable pentest targets directly to the public internet without strict firewall rules. Always scope inbound traffic limits within the OpenTofu security group definitions to prevent external exploitation of your lab.

Phase 2: Generating Dynamic Inventory for Ansible

A frequent friction point in IaC workflows is transferring the IP addresses of newly created VPS instances into Ansible. To bridge this gap, OpenTofu's local-exec provisioners or output variables can be utilized to automatically generate an Ansible-compatible inventory file immediately upon successful infrastructure creation. This eliminates manual copy-pasting and ensures a seamless handoff between provisioning and configuration.

Phase 3: Orchestrating the Lab Configuration via Ansible

With the infrastructure live and indexed, Ansible executes targeted roles across the instances. The playbook structure is divided into two primary execution paths:

  1. The Attacker Node Configuration: Installs core command-line tools, sets up container runtimes, configures VPN gateways (e.g., WireGuard) for secure remote access, and pre-downloads custom wordlists and scanning scripts.
  2. The Target Node Configuration: Utilizes Docker and Docker Compose to spin up diverse, isolated vulnerable environments. Docker is highly recommended here, as it allows multiple vulnerable web applications and legacy operating systems to run concurrently without conflicting dependencies.
---

Optimizing for a 'One-Click' Execution Workflow

To truly achieve a 'One-Click' experience, the individual invocation of OpenTofu and Ansible should be encapsulated into a single execution controller. This can be achieved through a unified Makefile or a master shell script. Consider the following workflow automation snippet:

#!/bin/bashset -eecho "[1/3] Initializing and applying OpenTofu infrastructure..."tofu init && tofu apply -auto-approveecho "[2/3] Waiting for SSH availability on remote nodes..."tofu output -json | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | xargs -I {} xterm -e "nc -zv {} 22"echo "[3/3] Executing Ansible playbooks for environment configuration..."ansible-playbook -i inventory.ini site.ymlcat << 'EOF'██╗      █████╗ ██████╗     ██████╗ ███████╗██████╗ ██╗      ██████╗ ██╗   ██╗██║     ██╔══██╗██╔══██╗    ██╔══██╗██╔════╝██╔══██╗██║     ██╔═══██╗╚██╗ ██╔╝██║     ███████║██████╔╝    ██║  ██║█████╗  ██████╔╝██║     ██║   ██║ ╚████╔╝ ██║     ██╔══██║██╔══██╗    ██║  ██║██╔══╝  ██╔═══╝ ██║     ██║   ██║  ╚██╔╝  ███████╗██║  ██║██████╔╝    ██████╔╝███████╗██║     ███████╗╚██████╔╝   ██║   ╚══════╝╚═╝  ╚═╝╚═════╝     ╚═════╝ ╚══════╝╚═╝     ╚══════╝ ╚═════╝    ╚═╝   DEPLOYMENT COMPLETE: Your secure pentest environment is ready for operation.EOF
---

Operational and Security Best Practices

When running a persistent or ephemeral pentest lab on a public VPS, adhering to operational guardrails is vital to protect your infrastructure and comply with cloud provider Terms of Service (ToS).

  • Implement Ephemerality: Design your lab to be temporary. Use the tofu destroy command to tear down the entire environment immediately after an assessment concludes. This drastically minimizes costs and reduces the window of exposure for vulnerable applications.
  • State File Security: The OpenTofu state file (terraform.tfstate) contains sensitive system metadata and sensitive variables. Ensure this file is stored securely using encrypted remote backends (e.g., AWS S3 with KMS or GitLab managed state) and never commit it to public version control systems.
  • Provider Notification: Some cloud vendors flag automated creation of vulnerable systems or scanning activities as malicious. Always check your provider's policy regarding penetration testing or utilize providers that explicitly permit security research labs.
---

Conclusion

Embracing OpenTofu and Ansible to establish a 'One-Click Deploy' infrastructure transforms the way security teams manage validation environments. It replaces manual, fragile setups with a reliable, auditable, and standard framework. By defining your security architecture as code, you gain the agility to spin up complex networks on-demand, execute tests against uncorrupted environments, and tear them down rapidly. This standardized automation ultimately allows engineers to shift their focus away from systemic troubleshooting and dedicate their resources entirely to what matters most: identifying and neutralizing security risks.

Automating Pentest Lab Deployments: A One-Click OpenTofu and Ansible Framework on VPS | DPTCloud