Vagrant is a piece of software, that allows for easy building and tearing down of virtual machines from the command line. This makes the tool very good for spinning up, and tearing down test environments fast.
It can work with libvirt (QEMU), hyperV, Virtualbox and others. Though my personal testing, seems to show it working best with Virtualbox
Note that while Vagrant itself can work with multiple hypervisors, the image used has to be made for the chosen hypervisor.
Images can be found here: https://app.vagrantup.com/boxes/search There are both official images, and usercreated images (like dockerhub), so be mindful of which image is chosen.
Vagrant is in most distro’s repositories, so installing it is as simple as using the package manager.
URL: https://developer.hashicorp.com/vagrant
For virtualbox Vagrant will place the disks for the VMs in the virtualbox default disk folder. This folder can be changed in Virtualbox preferences.
Once vagrant is installed, we should create a folder where we want to keep our vagrant files (the files we use to define how we want the vm set up.)
Inside this folder, we should create subfolders for the vagrant boxes we want to use. Each folder should house its own Vagrantfile.
vagrant]$ tree
.
├── README.md
├── vagrant_MultiVM_Example
│ ├── files
│ │ └── vagrantKey.pub
│ ├── playbook.yml
│ ├── todo.md
│ └── Vagrantfile
└── vagrant_SingleVM_Example
├── files
│ └── vagrantKey.pub
├── playbook.yml
├── todo.md
└── Vagrantfile
We can either build the Vagrantfile from scratch, or we can have a default file created for us with the command “vagrant init”.
We can either build the Vagrantfile from scratch, or we can have a default file created for us with the command “vagrant init”. If we want to build the Vagrantfile from scratch, we should first take a look at the Vagrantfile documentation found here: https://developer.hashicorp.com/vagrant/docs/vagrantfile .
One of the smart features of Vagrant is its ability to configure a VM after it has been brought up. This is called Provisioning in Vagrant terms. There are a few options for how Vagrant can handle this. Amongst them are “Shell commands”, “Puppet”, “Chef” and “Ansible”. Since I prefer ansible, that is what I will focus on here.
Telling Vagrant to use ansible to provision our virtual machines consists of two parts.
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end