Design and Test Ansible Playbooks with Vagrant
Testing infrastructure changes in production is risky. A disposable local test environment lets you validate Ansible playbooks safely before deploying to real servers.
Why You Need a Test Environment
Risk Mitigation: Catch errors before they affect production systems.
Faster Iteration: Test and refine playbooks without waiting for cloud resources.
Cost Savings: No cloud costs for development and testing.
Confidence: Deploy with certainty that your automation works.
Vagrant for Ansible Testing
Vagrant provides:
- Quick VM provisioning
- Reproducible environments
- Integration with Ansible
- Multi-machine setups
Workflow
- Define Infrastructure: Create a
Vagrantfiledescribing your VMs - Provision with Ansible: Use Vagrant’s Ansible provisioner
- Test Changes: Run playbooks against local VMs
- Iterate Quickly: Destroy and recreate as needed
- Deploy with Confidence: Apply tested playbooks to production
Example Structure
Vagrant.configure("2") do |config|
config.vm.define "webserver" do |web|
web.vm.box = "ubuntu/focal64"
web.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
end
Best Practices
- Match Production: Use similar OS versions and configurations
- Version Control: Track Vagrantfiles alongside playbooks
- Automate Testing: Integrate with CI/CD pipelines
- Document: Keep setup instructions with your code
The complete example code is available in .articles/1_vagrant_101 directory.
Read the full article on Medium: Design and Test Ansible playbook with Vagrant