· 8 min read
Dev Equals Prod: Reproducible Environments with Vagrant, Ansible and Docker
- DevOps
- Ansible
- Docker
- AWS
- Infrastructure as Code
The problem: "it works on my machine"#
Most production incidents I have seen in small teams were not caused by complex code. They were caused by environments drifting apart: a different OS package version on the server, a firewall rule that only existed in staging, a config file edited by hand at 2 a.m.
At Humatects I worked on automating network simulations and AWS deployments with Vagrant, Ansible and Docker, with one goal: development and production should be built from the same definitions. This post explains the pattern.
The three layers#
Each tool has a clear job:
| Tool | Responsibility |
|---|---|
| Vagrant | Creates disposable virtual machines locally, including multi-machine networks |
| Ansible | Configures any machine (VM, bare metal, EC2) into a known state, idempotently |
| Docker | Packages the application and its runtime so it runs identically everywhere |
The key insight is that Ansible is the shared layer. The same playbook that configures a local Vagrant VM also configures an EC2 instance. Vagrant and AWS are just two different ways to get a blank machine.
Step 1: a multi-machine lab with Vagrant#
Vagrant is particularly useful when you need to simulate a network, not just a single server, for example a gateway, an application host and a client on separate subnets:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.define "gateway" do |gw|
gw.vm.network "private_network", ip: "10.10.0.1", virtualbox__intnet: "lan"
gw.vm.network "private_network", ip: "10.20.0.1", virtualbox__intnet: "dmz"
end
config.vm.define "app" do |app|
app.vm.network "private_network", ip: "10.20.0.10", virtualbox__intnet: "dmz"
end
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/site.yml"
ansible.groups = { "gateways" => ["gateway"], "apps" => ["app"] }
end
end
vagrant up now builds the whole topology and hands each machine to Ansible.
Step 2: one playbook, many targets#
The playbook knows nothing about Vagrant or AWS. It only knows roles:
# ansible/site.yml
- hosts: gateways
become: true
roles: [common, firewall, vpn]
- hosts: apps
become: true
roles: [common, docker, app]
For AWS, only the inventory changes. With the amazon.aws.aws_ec2 dynamic inventory plugin, instances are grouped by tag:
# inventories/aws/aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions: [eu-central-1]
keyed_groups:
- key: tags.Role
prefix: ""
separator: ""
An instance tagged Role=apps lands in the apps group and receives exactly the configuration a local VM gets.
Step 3: the application ships as a container#
The app role does very little: it installs Docker, pulls a versioned image and starts it with Docker Compose. The application itself is defined once, in a multi-stage Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /out
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /out .
USER app
ENTRYPOINT ["dotnet", "Api.dll"]
Because the image is built once in CI and pulled everywhere, "dev" and "prod" literally run the same bytes.
Step 4: make drift visible#
Reproducibility only holds if nobody changes servers by hand. Two habits enforce that:
- Run Ansible in check mode on a schedule.
ansible-playbook site.yml --check --diffreports any server that no longer matches the code. - Treat machines as disposable. If something is wrong, rebuild it from the playbook instead of patching it live.
What this buys you#
- New team members get a working environment with
vagrant up. - Network changes (firewall rules, VPN tunnels) can be tested on a laptop before they touch a real network.
- Production deployments become boring: the same playbook has already run dozens of times locally.
- Security hardening lives in code, so it is reviewed like code.
When to reach for something else#
For fully containerised workloads on Kubernetes, Vagrant and Ansible play a smaller role, and Terraform plus Helm usually take over provisioning. But for mixed estates (VMs, gateways, on-premise hardware and cloud instances side by side), this combination remains one of the most practical ways to keep every environment honest.