I’ve recently started building chef scripts to manage server configurations in Azure. This is a nice way of creating VMs that can then make use of the Azure autoscale features. I did not however have a convenient way of quickly testing these scripts when I made some modifications. Therefore I decided to use Vagrant to test these scripts before deploying them. In this post I’ll be detailing that process.
Vagrant is a really neat virtual machine manager. It works with VirtualBox to allow you to quickly run virtual machines whose configuration you specify in various Vagrantfiles. Vagrant manages downloading and installing the OS, and configuring it the way you want it. Then in the config file you just specify what you want Vagrant to do with the VM (in our case, run some chef scripts) and it’ll do it for you.
The best thing about all of those is that it’s really easy to do. Once you have a config file all you need to do is run a quick
vagrant up to start the VM. If you’ve already created it simply running
vagrant provision will restart the VM and execute your updated scripts. Pretty handy.
Install VirtualBox and Vagrant
I’ll be using VirtualBox to provision VMs with Vagrant, so first we’ll need to install it. I’m running Ubuntu so the easiest way to do that is to open the Ubuntu Software Center, search for “VirtualBox”, and click install. Alternatively you could run
apt-get install virtualbox. While that happens, lets get Vagrant.
Head to the Download Vagrant page and copy the link to the installer for your OS/architecture. I’ll be using the Ubuntu 64bit package. If you’re using another one, replace the link in the
wget command. You will also need to wait for the VirtualBox installation to finish before running the
dpkg command to install Vagrant.
wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.deb sudo dpkg -i vagrant_*.deb
Note there is a vagrant package available in the Ubuntu source repos, but I had problems with it. For best results I recommend installing the package directly from Vagrant.
Next step is to configure Vagrant to create the VM and run your chef scripts. We’ll start by creating a directory with a base Vagrantfile:
mkdir vagrant_chef_test cd vagrant_chef_test vagrant init hashicorp/precise32
This command will create a base Vagrantfile setup to install the precise32 (Ubuntu) image. You can choose another image here if you like. Take a look at the Vagrant Boxes if you want something else.
Next open the created Vagrantfile in your favourite text editor, and add the following lines after
Vagrant.configure(2) do |config|:
config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "../chef_cookbooks" chef.add_recipe("my_cookbook") end
chef.cookbooks_path is the path to your chef cookbooks, and
my_cookbook is the name of the chef cookbook you want to run.
Now to run the Vagrant VM and test everything. In the
vagrant_chef_test directory containing your Vagrantfile run:
If all goes well Vagrant will install the box you’ve asked for, install the Chef extension, and run your cookbook. You’ll be able to see the output of all of this in your command window.
After you’ve made some changes to your chef scripts and would like to test again you don’t necessarily need to
vagrant destroy and
vagrant up (unless you want to test provisioning from zero), you can just provision to re-run your chef scripts:
There is also a vagrant extension to install cookbooks using berks if you require. First, install the berks plugin for vagrant:
vagrant plugin install vagrant-berkshelf
Now edit your
Vagrantfile and add the following before
config.vm.provision :chef_solo do |chef|:
config.berkshelf.enabled = true config.berkshelf.berksfile_path = "../chef_cookbooks/my_cookbook/Berksfile"
config.berkshelf.berksfile_path is the path to the Berksfile in your cookbook.