Provisioning virtual machines using vagrant and chef-solo
Recently I’ve re-discovered the brilliant tool called Vagrant that could be used to very quickly provision new virtual machines (VMs). You only need to define the settings of the VM in one file and it will generate the machine based on a given box-template (in most cases a certain version of Ubuntu Linux).
The installation and configuration of software is done by including pre-built or self-made instruction files, in the case of chef-solo (there are further options, but I prefer chef) called recipes.
For example if you need a test environment for Ruby on Rails applications you could simply create a own recipe, that installs the needed software. There are a lot of mostly good maintained cookbooks (collections of recipes) on the GitHub profile of the creator of chef. At the beginning of my experiments I’ve used them directly from GitHub, but now I would absolutly recommend not using the bleeding-edge-versions but rather the official released, stable ones. There is also a very nice way to manage the dependencies of your vm using the Berkshelf gem and the corresponding Vagrant plugin.
Booth could be simply installed:
gem install berkshelf
vagrant plugin install vagrant-berkshelf
Then you could create a
Berksfile that’s similar to a Gemfile in a Ruby project using bundler:
site :opscode cookbook 'apt' cookbook 'build-essential' cookbook 'vim' cookbook 'git' cookbook 'mysql' cookbook 'rvm', github: 'fnichol/chef-rvm' cookbook 'rails_development', path: 'cookbooks/rails_development'
Beside that you also need to use this cookbooks in your
[...] config.berkshelf.enabled = true [...] config.vm.provision :chef_solo do |chef| chef.add_recipe "apt" chef.add_recipe "build-essential" [...] end
Or reference them as dependency in your own cookbook:
[...] depends 'apt' depends 'build-essential' depends 'sudo' depends 'vim' depends 'git' depends 'mysql' [...]
[...] include_recipe 'apt' include_recipe 'build-essential' include_recipe 'sudo' include_recipe 'vim' include_recipe 'git' include_recipe 'mysql::server' include_recipe 'rvm::user' [...]
After the execution of the
vagrant up command inside your VMs project-folder (the one containing the
Vagrantfile) Vagrant will get all recipes, create the VM, configure it, boot it and afterwards provision it using the given recipes with chef-solo.