Skip to content →

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.

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.

The vagrant-vsphere plugin has been created by the National Snow and Ice Data Center team and open sourced for others to use. You can read more information about it on the Github page for the project at github.com/nsidc/vagrant-vsphere/. There are more configuration options that I don’t mention here that you can tinker with once you are more familiar with how the setup works.

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:

Install all the updates and then reboot:

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

Again, after they’re installed, reboot:

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

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

At the end of the file, add the line:

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

After the file has downloaded, reset permissions to the correct ones:

Now we’ll need to install the OpenSSH server (if you haven’t already):

After installation is complete, edit the configuration file at /etc/ssh/sshd_config and make sure that following options match the below:

Restart the SSH server:

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.

Creating a Template in vSphere

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:

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:

Save that file, double check your credentials and then run Vagrant:

You can see progress in the task/event log in vSphere, but you should see the template being cloned and then the machine booting:

vSphere showing created VM and clone in operation

After this has completed, you’ll have something similar to the following output where you ran vagrant up:

You can now use the other Vagrant commands such as ssh:

and of course you can vagrant destroy too:

I’ll most likely update this article soon with provisioning Nginx, PHP etc. If you need a dummy box to use with vagrant-vsphere, then I’ve added one to Vagrant Cloud at:

https://vagrantcloud.com/ssx/boxes/vsphere-dummy/versions/1/providers/vsphere.box

Published in Uncategorized