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, 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.
Easy, reproducible build of an Ubuntu 14.04LTS (trusty) VirtualBox VM with the Chef management tools installed.
Installed and configured on your system:
- Vagrant v1.6.x+; and
- VirtualBox
Note that 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!
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 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.
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:
[gist a328b4d542d8c731b067 file="Vagrantfile.minimal" /]
If you want to explore a bit, leaving off the --minimal creates a substantially larger Vagrantfile.
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:
[gist a328b4d542d8c731b067 file="provision.sh" /]
...and configure Vagrant:
[gist a328b4d542d8c731b067 file="Vagrantfile" /]
Now, all we should need to do is "vagrant up" in order to launch and provision our VM:
[gist a328b4d542d8c731b067 file="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":
[gist a328b4d542d8c731b067 file="vagrant-status.log" /]
To do this you can use the 'client-configure.sh' script. Note that 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 as such:
[gist a328b4d542d8c731b067 file="vagrant-client-configure.log" /]
...for brevity, the above output has been trimmed; it should mirror the description you see from the previous tutorial.
Now, whenever you need to use your Chef management tools, you can simply "vagrant ssh" into this VM.
Enjoy!