In our previous tutorial Installing Chef on the CenturyLink Cloud, we showed how to set up three hosts: one as a Chef server, one as management workstation (client), and one as a Chef-managed node.

However, sometimes it's useful to have the Chef client on your local workstation. Given that the Chef client install feels somewhat...invasive...for a machine used as a primary workstation – you know, the machine you absolutely and without hesitation depend on in order to access and manage everything else – this seems like an excellent opportunity to examine using Vagrant in conjunction with a virtualization provider to provide for an easy, repeatable way to create contained VM's usable as Chef management interfaces.

Goals

Easy, reproducible build of an Ubuntu 14.04LTS (trusty) VirtualBox VM with the Chef management tools installed.

Prerequisites

Installed and configured on your system:

Note: If you're using Ubuntu Trusty (14.04LTS) the latest level of vagrant available through the Ubuntu apt repositories is 1.4.3, as of this writing. Using a different virtualization provider is left as an exercise for a willing reader.

The files (and some others) described in this article can be obtained by cloning this repository:

$ git clone https://github.com/RsrchBoy/ctl-vagrant-encapsulating-chef.git

Ok, ready? Go!

Picking a base box

A Vagrant base box is basically a template box; it's used as the foundation of the box you'll be creating. If you're familiar with how Docker images are built then this concept is very similar to the "FROM" line in a Dockerfile. Thanks to the Vagrant Cloud, there are a large number of publicly accessible base boxes available, and the Ubuntu team has an official 64-bit trusty base box they maintain.

Creating our initial Vagrantfile

Create a minimal initial Vagrantfile with:

$ vagrant init --minimal ubuntu/trusty64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

...this yields a Vagrantfile.minimal file that looks like this:

# -*- mode: ruby -*-
# vi: set ft=ruby ts=2 :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"
end

If you want to explore a bit, leaving off the --minimal creates a substantially larger Vagrantfile. You can check it out here.

Provision our VM

Now comes the fun part. Basically, we take all the steps outlined in the previous tutorial and have Vagrant execute them inside the virtual machine as part of the provisioning process.

The initial Vagrantfile created above contains only the barebones minimum required to launch a new virtual machine based off of the ubuntu/trusty64 image. (And a lot of comments!)

Let's stick all of our needed provisioning into one script: provision.sh

#!/bin/sh
#
# Provision our VM with Chef tools.
#
# Chris Weyl <[email protected]> 2014

apt-get install -y git

curl -L https://www.opscode.com/chef/install.sh | bash
chef-client -v

# ensure our path is configured
echo 'export PATH="/opt/chef/embedded/bin:$PATH"' > /etc/profile.d/chef.sh

...and configure Vagrant using our Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby ts=2 :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # give our box a useful name
  config.vm.define 'ctl/chef-mgmt'

  config.vm.box = 'ubuntu/trusty64'
  config.vm.hostname = 'chef-mgmt'
  config.ssh.forward_agent = true

  config.vm.provision :shell, path: 'provision.sh'
end

Fire it up!

Now, all we should need to do is vagrant up in order to launch and provision our VM: vagrant-up.log

There's a lot of noise generated by Chef's installation script, so I've omitted it from the log, above. To check the status, you can run vagrant status:

$ vagrant status
Current machine states:

ctl/chef-mgmt             running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

Configuring the Chef Client

To do this you can use the client-configure.sh script. The directory containing the Vagrantfile is exported into the VM at '/vagrant', so we can log in to our newly-created VM and configure the client. Note: You'll need to set the appropriate servername or IP address

$ ./client.configure.sh server.hostname.or.ip

...for brevity, the above output has been trimmed; it should mirror the description you see from the previous tutorial.

$ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-30-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sun Sep 21 23:15:09 UTC 2014

  System load:  0.0               Processes:           72
  Usage of /:   3.1% of 39.34GB   Users logged in:     0
  Memory usage: 24%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud


Last login: Sun Sep 21 23:15:10 2014 from 10.0.2.2
[email protected]:~$ ls /vagrant
000-article.md  client-configure.sh  provision.sh  Vagrantfile  Vagrantfile.minimal  Vagrantfile.with-comments  vagrant-status.log  vagrant-up.log
[email protected]:~$ sh /vagrant/client-configure.sh
Cloning into 'chef-repo'...
remote: Counting objects: 225, done.
remote: Total 225 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (225/225), 46.12 KiB | 0 bytes/s, done.
Resolving deltas: 100% (58/58), done.
Checking connectivity... done.
# ... etc

Enjoy!

Now, whenever you need to use your Chef management tools, you can simply "vagrant ssh" into this VM. There are a great many things one can do to optimize this to meet ones personal tastes – or have the "provisioning" stage fully configure for a given Chef environment – and these are left as exercises for the readers. Enjoy!

What's Next?

We give you the deployment tools you need to manage your applications quickly and easily. Check out our Knowledge Base of articles on CenturyLink Cloud. We also have several container tools listed in our Developer Center.

Not a CenturyLink Cloud customer? No problem. Migrate to the CenturyLink Cloud Platform with free on-boarding assistance and receive a matching spend credit based on your commitment for your initial period of platform use with us.

Sign up for our Developer-focused newsletter CODE. Designed hands-on by developers, for developers. Keep up to date on topics of interest: tutorials, tips and tricks, and community building events.

We’re a different kind of cloud provider – let us show you why.