Managing virtual machines with Vagrant
Vagrant is a command-line tool that provides you with a configurable, reproducible, and portable development environment using VMs. It lets you define and use preconfigured disk images to create new VMs from. Also, you can configure Vagrant to use provisioners such as Shell scripts, Puppet, or Chef to bring your VM into the desired state.
Tip
Chef comes with Test Kitchen, which enables you to test your cookbooks on Vagrant without you needing to setup anything manually.
You only need to follow this section, if you want to learn how to use Vagrant and Chef for more advanced cases.
In this recipe, we will see how to use Vagrant to manage VMs using VirtualBox and Chef client as the provisioner.
Getting ready
- Download and install VirtualBox at https://www.virtualbox.org/wiki/Downloads.
- Download and install Vagrant at https://www.vagrantup.com/downloads.html.
- Install the Omnibus Vagrant plugin to enable Vagrant to install the Chef client on your VM by running the following command:
mma@laptop:~/chef-repo $ vagrant plugin install vagrant-omnibus Installing the 'vagrant-omnibus' plugin. This can take a few minutes... Installed the plugin 'vagrant-omnibus (1.5.0)'!
How to do it…
Let's create and boot a virtual node by using Vagrant:
- Visit https://github.com/chef/bento and choose a Vagrant box to base your VMs on. We'll use the amd64 image of ubuntu-16.04 in this example.
- The URL of that box is http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-16.04_chef-provisionerless.box.
- Create a new
Vagrantfile
. Make sure that you replace<YOUR-ORG>
with the name of your organization on the Chef server. Use the name and URL of the box file you noted down in the first step asconfig.vm.box
andconfig.vm.box_url
:mma@laptop:~/chef-repo $ subl Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "opscode-ubuntu-16.04" config.vm.box_url = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-16.04_chef-provisionerless.box" config.omnibus.chef_version = :latest config.vm.provision :chef_client do |chef| chef.provisioning_path = "/etc/chef" chef.chef_server_url = "https://api.chef.io/organizations/<YOUR_ORG>" chef.validation_key_path = ".chef/<YOUR_USER>.pem" chef.validation_client_name = "<YOUR_USER> " chef.node_name = "server" end end
- Create your virtual node using Vagrant:
mma@laptop:~/chef-repo $ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'opscode-ubuntu-16.04' could not be found. Attempting to find and install... ...TRUNCATED OUTPUT... ==> default: Importing base box 'opscode-ubuntu-16.04'... ...TRUNCATED OUTPUT... ==> default: Installing Chef latest Omnibus package... ...TRUNCATED OUTPUT... ==> default: Running chef-client... ==> default: Starting Chef Client, version 12.14.89 ...TRUNCATED OUTPUT...
- Log in to your virtual node using SSH:
mma@laptop:~/chef-repo $ vagrant ssh Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-31-generic x86_64) ...TRUNCATED OUTPUT... vagrant@server:~$
- Log out of your virtual node:
vagrant@server:~$ exit logout Connection to 127.0.0.1 closed. mma@laptop:~/chef-repo $
- Validate that the Chef server knows your new virtual machine as a client called server:
mma@laptop:~/chef-repo $ knife client list awo-validator server
- Go to https://manage.chef.io/organizations/
<YOUR ORGANIZATION>/nodes
and validate that your new virtual machine shows up as a registered node:
How it works…
The Vagrantfile
is written in a Ruby Domain Specific Language (DSL) to configure the Vagrant virtual machines. We want to boot a simple Ubuntu VM. Let's go through the Vagrantfile
step by step.
First, we create a config
object. Vagrant will use this config
object to configure the VM:
Vagrant.configure("2") do |config| ... end
Inside the config
block, we tell Vagrant which VM image to use, in order to boot the node:
config.vm.box = "opscode-ubuntu-16.04" config.vm.box_url = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-16.04_chef-provisionerless.box"
We want to boot our VM using a so-called Bento Box, provided by Chef. We use Ubuntu Version 16.04 here.
Note
If you have never used the box before, Vagrant will download the image file (a few hundred megabytes) when you run vagrant up
for the first time.
As we want our VM to have the Chef client installed, we tell the omnibus vagrant plugin to use the latest version of the Chef client:
config.omnibus.chef_version = :latest
After selecting the VM image to boot, we configure how to provision the box by using Chef. The Chef configuration happens in a nested Ruby block:
config.vm.provision :chef_client do |chef| ... end
Inside this chef
block, we need to instruct Vagrant on how to hook up our virtual node to the Chef server. First, we need to tell Vagrant where to store all the Chef stuff on your node:
chef.provisioning_path = "/etc/chef"
Vagrant needs to know the API endpoint of your Chef server. If you use hosted Chef, it is https://api.chef.io/organizations/<YOUR_ORG>
. You need to replace <YOUR_ORG>
with the name of the organization that you created in your account on hosted Chef. If you are using your own Chef server, change the URL accordingly:
chef.chef_server_url = "https://api.chef.io/organizations/<YOUR_ORG>"
While creating your user on hosted Chef, you must have downloaded your private key. Tell Vagrant where to find this file:
chef.validation_key_path = ".chef/<YOUR_USER>.pem"
Also, you need to tell Vagrant which client it should use to validate itself against in the Chef server:
chef.validation_client_name = "<YOUR_USER>"
Finally, you should tell Vagrant how to name your node:
chef.node_name = "server"
After configuring your Vagrantfile
, all you need to do is run the basic Vagrant commands such as vagrant up
, vagrant provision
, and vagrant ssh
. To stop your VM, just run the vagrant halt
command.
There's more…
If you want to start from scratch again, you will have to destroy your VM and delete both the client and the node from your Chef server by running the following command:
mma@laptop:~/chef-repo $ vagrant destroy mma@laptop:~/chef-repo $ knife node delete server -y && knife client delete server -y
Alternatively, you may use the Vagrant Butcher plugin found at https://github.com/cassianoleal/vagrant-butcher.
Tip
Don't blindly trust Vagrant boxes downloaded from the Web; you never know what they contain.
See also
- Integration-testing your Chef cookbooks with Test Kitchen in Chapter 2, Evaluating and Troubleshooting Cookbooks and Chef Runs
- Find the Vagrant documentation at https://www.vagrantup.com/docs/getting-started/index.html
- You can use a Vagrant plugin for VMware instead of VirtualBox and find it at http://www.vagrantup.com/vmware
- You can use a Vagrant plugin for Amazon AWS instead of VirtualBox and find it at https://github.com/mitchellh/vagrant-aws