Please note: This post assumes you are comfortable with Vagrant, Visualisation 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:
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 -
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:
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:
mkdir -p /home/vagrant/.ssh
wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
After the file has downloaded, reset permissions to the correct ones:
chmod 0700 /home/vagrant/.ssh
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh
Now we'll need to install the OpenSSH server (if you haven't already):
sudo apt-get install -y openssh-server
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:
sudo service ssh restart
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
Vagrant.configure("2") do |config|
config.vm.box = 'vsphere'
config.vm.box_url = 'https://vagrantcloud.com/ssx/boxes/vsphere-dummy/versions/1/providers/vsphere.box'
config.vm.provider :vsphere do |vsphere|
# The host we're going to connect to
vsphere.host = '192.168.1.253'
# The host for the new VM
vsphere.compute_resource_name = '192.168.1.2'
# The resource pool for the new VM
vsphere.resource_pool_name = 'Linux'
# The template we're going to clone
vsphere.template_name = 'ubuntu.template.dc.sw10.net'
# The name of the new machine
vsphere.name = 'bingo'
# vSphere login
vsphere.user = 'root'
# vSphere password
vsphere.password = 'password'
# If you don't have SSL configured correctly, set this to 'true'
vsphere.insecure = true
Save that file, double check your credentials and then run
vagrant up --provider=vsphere
You can see progress in the task/event log in vSphere, but you should see the template being cloned and then the machine booting:
After this has completed, you'll have something similar to the following output where you ran
➜ vagrant up --provider=vsphere
Bringing machine 'default' up with 'vsphere' provider...
==> default: Calling vSphere CloneVM with the following settings:
==> default: -- Template VM: dc.sw10.net/vm/ubuntu.template.dc.sw10.net
==> default: -- Target VM: dc.sw10.net/vm/bingo
==> default: Waiting for SSH to become available...
==> default: New virtual machine successfully cloned and started
==> default: Rsyncing folder: /Users/scott/Downloads/tmp/ => /vagrant
You can now use the other
Vagrant commands such as ssh:
➜ tmp vagrant ssh
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-32-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information disabled due to load higher than 1.0
Last login: Sat Oct 11 13:30:21 2014 from 192.168.1.3
and of course you can
vagrant destroy too:
➜ vagrant destroy
==> default: Calling vSphere PowerOff
==> default: Calling vSphere Destroy
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: