Design and Test Ansible Playbooks with Vagrant

A disposable local test environment is essential for DevOps. Learn how to use Vagrant to safely test Ansible playbooks before production deployment.

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

  1. Define Infrastructure: Create a Vagrantfile describing your VMs
  2. Provision with Ansible: Use Vagrant’s Ansible provisioner
  3. Test Changes: Run playbooks against local VMs
  4. Iterate Quickly: Destroy and recreate as needed
  5. 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