Using VMware vSphere/ESXi as a Provider for Vagrant

Please note: This post assumes you are comfortable with Vagrant, Virtualisation and generic admin tasks on a server.

October 11, 2014

~

Please note: This post assumes you are comfortable with Vagrant, Virtualisation and generic admin tasks on a server.

vSphere & ESXi

One thing I’ve been wanting to do for a while was control my VMWare ESXi based hosts with Vagrant, so that I could easily create and remove boxes while testing. By default, Vagrant can’t do this. Similar to how the interaction with Digital Ocean works, you can install a plugin to aid in getting this to work.

To start, grab the ISO file of your distribution of choice. I chose to go with Ubuntu 14.04 as it is an LTS version and more recently the projects I’ve been working on have used it. You’re going to create a Virtual Machine that will serve as a base for all of your Vagrant instances that you’ll create in future.

Upload the Ubuntu ISO to your datastore and remember where you placed it. Next, create a new virtual machine with a reasonable amount of RAM and CPUs for your box, select the ISO in the CDROM options and boot up your new virtual machine as normal. Set up the box as you usually would do, I left pretty much everything as default apart from the user which you’ll need to setup for Vagrant to use.

During the install you will be prompted to create a user account. Use the username vagrant and the password vagrant, this is required for vagrant to run later down the line.

Complete the installation process, installing any additional software that you would like such as Nginx, Apache, MySQL etc.

After the machine has rebooted, login and run the update commands:

sudo apt-get update -y
sudo apt-get upgrade -y

Install all the updates and then reboot:

sudo shutdown -r now

Next, install the open-vm-tools package which provides VMWare integration for the guest:

sudo apt-get install open-vm-tools -y

Again, after they’re installed, reboot:

sudo shutdown -r now

Next, you need to add the vagrant user to the sudoers file:

sudo su -

Enter the vagrant user password that you set to vagrant earlier and then run the following command to edit the sudoers for the machine:

visudo

At the end of the file, add the line:

vagrant ALL=(ALL) NOPASSWD:ALL

Exit the editor by pressing CTRL+X, Save changes. Next we need to add Vagrants key to the user account:

Now we have our template box. In your vSphere client, right click on your newly created virtual machine, select Template, then select “Clone to Template”. Follow the steps and give it a good name that you’ll remember.

As you can see in the image above, I called mine ubuntu.template.dc.sw10.net which is sitting in the resource pool Linux in the host 192.168.1.2 and that host resides in the datacentre dc.sw10.net. This is just a sample configuration and you can adjust yours to suit.

Installing the Vagrant Plugin

If you’ve got this far, I’m going to assume that you have Vagrant installed on your on your machine. Run the following command to install the plugin that we’re going to use:

vagrant plugin install vagrant-vsphere

You can learn more about the plugin and it’s various options over on the Github page at github.com/nsidc/vagrant-vsphere/. There are quite a few settings that you can change which I won’t detail here.

Creating a Vagrantfile

To create a Vagrantfile that we can run, we’ll need to add a few configuration options in to instruct the plugin on where to locate things. I started with the following Vagrantfile: