Vagrant is a tool made by hashicorp for provisioning and deprovisioning virtual machines and groups of virtual machines. Frequently Vagrant is used for brining up test machines on your local computer. But, sometimes you want to run your virtual machines in your datacenter. In this article, I will describe how to use Vagrant with vmware VCenter to provision and de-provision virtual machines running in your datacenter.
As you are running through this article, you will need to download various files. I recommend creating a vagrant folder on your computer and saving all files there.
Download and install Vagrant
First thing is to download and install Vagrant. You can download Vagrant here.
After Vagrant is downloaded, simply double click on the installer icon, and follow the wizard to complete the setup.
Alternatively, if you have chocolatey installed on your computer, you can install Vagrant with the command:Choco install vagrant.
For more information on chocolatey, see our article on installing chocolatey.
Install Vagrant vCenter Provider
The vagrant-vsphere Vagrant plugin has a prerequisite of nokogiri. Nokogiri is an XML/HTML reader and parser. If you already have ruby installed, you can install Nokoko Giri with the following command:
gem install nokogiri
Now that the prerequisites are installed, we can install the vagrant-vsphere provider with the following command:vagrant plugin install vagrant-vsphere
Creating Dummy Box File
If you were using Vagrant on your local computer, you would go to Vagrants website and download a .box file. Or, you would use packer to create a new box file. You then reference that box file when you are provisoining machines, and Vagant builds a machine from that box file based on your specifications.
When you are using vSphere, things work a bit differently. When using the vSphere provider, we create a “Dummy Box”. This box is really just a placeholder to make Vagrant happy. It then pipes all the commands through to vsphere using rbvmomi, the ruby VMWare programming library.
To create the dummy box, you first need to download the metadata.json file found here. After you have downloaded the json file, open a terminal window, navigate to where you saved the file, and run the following command:tar cvzf dummy.box ./metadata.json
This will output a file called dummy.box into your vagrant folder.
Configuring vSphere provider
We now have all the bits downloaded and installed. Now we can start configuring Vagrant.start by creaing a new file in your Vagrant folder called vagrant file with no file extension. Open the file with your text enditor, and paste the following:
Vagrant.configure("2") do |config|
config.vm.box = 'vsphere'
config.vm.box_url = 'dummy.box'
config.ssh.private_key_path = ‘<Path to your SSH Private Key>'
config.vm.provider :vsphere do |vsphere|
vsphere.customization_spec_name = ‘<Name of Customization spec>'
vsphere.host = ‘<FQDN of vcenter server>'
vsphere.compute_resource_name = ‘<ESX Cluster Name>'
vsphere.resource_pool_name = ‘<Resource Pool Name>'
vsphere.template_name = ‘<Folder>/<Template Name>'
vsphere.name = ‘<What do you want to vall the VM when it is provisioned?>'
vsphere.user = ‘<vSpere username>'
vsphere.password = ‘<Password for vCenter>'
#Template you are cloning from must have a snapshot for this to work
vsphere.linked_clone = true
vsphere.insecure = true
Notice there are a few items you will need to fill in. Once thing to note, the option vspehre.linked_clone is an optional attribute. However, using a linked clone will make provisioning machines much faster. The only caveat is you have to take a snapshot of the virtual machine template while it is still a VM. Then all the linked clones will be based off that snapshot. If you update the template, you will need to remember to create a new snapshot. Otherwise future linked clones won’t see the updates you have made.
At this stage, Vagrant is installed and configured.
Creating a Vagrant Template in vSphere
There are not a lot of steps to create a template to use with Vagrant in vSphere. Below is my checklist for creating an Ubuntu vagrant template:
Create Virtual Machine
Create vagrant user using: useradd vagrant command
Create vagrant home and .ssh folder with the following command
Grant vagrant ownership permissions over folder
chmod vagrant:vagrant /home/vagrant
Add vagrant user to sudoer group
usermod -aG sudo vagrant
Add your public ssh key to the authorized_keys file for the vagrant user
echo “<Contents of your SSH public key>” >> /home/vagrant/.ssh/authorized_keys
Allow users to run sudo commands without a password:
- Run visudo command
- change the line starting with %sudo to read: %sudo ALL=(ALL:ALL NOPASSWD:ALL
- Press Ctrl + X to exit
Run visudo command
change the line starting with %sudo to read: %sudo ALL=(ALL:ALL NOPASSWD:ALL
Press Ctrl + X to exit
Install Updates on machine, run the following commands:
Apt-get update; apt-get upgrade
Install VMWare tools: sudo apt-get install open-vm-tools
Shut down virtual machine
Convert to template
VMWare customization specification
You need to have a vmware customization specification you can call. Generally I set the option to use the machine name as the host name and that is it. Vagrant can handle the rest. You will need to plug the name of this customization specification into your vagrant file before proceeding to the next step.
Using the vSphere provider
We have now completed our Vagrant setup and we are ready to start using it.There are 3 main commands you will need when using vagrant
- vagrant up – provisions one or more virtual machines based on your vagrant file configurations
- vagrant ssh – connects to your provisioned machine via SSH. by default it will try to use SSH key authentication. It is also possible to use password authentication. In a multi-machine configuration, you will need to specify the server name as well.
- vagrant destroy – Shuts down and deletes your provisoined VM’s.
If you have trouble connecting with the vagrant up command. You can try manually specifying the vSphere provider wit hthe following command:
vagrant up --provider=vcenter
In this article we installed and configured Vagrant to use the vSphere provider. You can now use the Vagrant up command and the Vagrant destroy command to instantly provision and deprovision virtual machines through the use of linked clones.