Skip to content

Ansible Local Infrastructure

Test Ansible infrastructure locally#

Test-driven development helped popularise infrastructure testing: test locally before touching live environments.

That gives you documented and reversible infrastructure changes.

Tools#

  • Vagrant: server provisioning tool
  • VirtualBox: local virtualisation environment

Set up Vagrant#

Download Vagrant.

Download VirtualBox.

Add a box from HashiCorp Vagrant Cloud.

Add and initialise a VM#

# Box management: add
vagrant box add <box_name> <box_url>

# Initialise a new Vagrant environment using a Vagrantfile
vagrant init <box_name>

# Boot the server
vagrant up

Basic Vagrant commands#

  • vagrant halt: shut down the VM
  • vagrant up: start the VM
  • vagrant ssh: SSH into the VM
  • vagrant ssh-config: get required SSH details
  • vagrant destroy: delete the machine from VirtualBox

Set Ansible as the provisioner#

In Vagrantfile:

# Provisioning configuration for Ansible.
config.vm.provision "ansible" do |ansible|
  ansible.playbook = "playbook.yml"
end