Using Vagrant with Puppet to create VirtualBox hosts

Vagrant integrates with Puppet and Chef to automate configuration management to build VirtualBox hosts. Linux systems admins can use this technology for quickly creating development hosts and production hosts.

Mitchell Hashimoto’s Vagrant was designed for developers to allow them to create simple and reusable VirtualBox-based virtual machines on their laptops for use as test hosts. But the tool has turned into an open source systems management favorite after system administrators in Dev/Ops environments saw its value for building reusable virtual machines for prototyping or as ready-made testing and staging hosts. Vagrant was built to integrate...

with Puppet and Chef to make use of configuration management automation to build the hosts. So, any configuration created for development hosts can also be used on production hosts.

Getting started with Vagrant
Let’s take a quick tour of Vagrant and show you how to install and get your first box configured using Puppet. First, we need to install Oracle’s Virtual Box virtualization platform. Download and install an appropriate package for your host. There are packages for Linux, OSX, Windows and Solaris. Here we’ll install the package for Red Hat Enterprise Linux 6.

$ wget http://download.virtualbox.org/virtualbox/4.0.4/VirtualBox-4.0-4.0.4_70112_rhel6-1.i686.rpm

$ sudo rpm -Uvh VirtualBox-4.0-4.0.4_70112_rhel6-1.i686.rpm

(Note: If you cannot install VirtualBox 4.x then note that VirtualBox 3.x versions only work with Vagrant 0.6.9 and earlier)

You will also need Ruby and RubyGems which you can easily install from your platform’s package manager (if they are not already installed), for example on RHEL 6 again:

$ sudo yum install ruby rubygems

Then using RubyGems you can install Vagrant itself:

$  sudo gem install vagrant

Vagrant relies on the concept of base boxes that you can download and then work with. To download one or more of them, refer to this list of the available boxes.

We’re going to download a base Ubuntu box as a start using the vagrant box command. First, create a directory:

$ mkdir /home/james/vagrant && cd /home/james/vagrant

Then add our Vagrant box:

$ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box

This will download a Lucid 32-bit (Ubuntu version 10.04 LTS) base box called lucid32. We can then initialize this box using the init command:

$ vagrant init

This will add the box to Vagrant and prepare it for start-up. We can then start it with the following command:

$ vagrant up

This will configure the Vagrant box and bring it up and make it available for use. Once it’s configured we can connect to it using the ssh command.

$ vagrant ssh

This will SSH into the vagrant box and you can interact and manage it like any other virtual machine. If you want to shut down your Vagrant box you have two options. The first suspends it:

$ vagrant suspend

This pauses the virtual machines and you can naturally resume it like so:

$ vagrant resume

But important you can also have a complete do-over and destroy your box:

$ destroy vagrant

This will reset the box back to its original configuration.  Any changes you made to the box or data you placed on it will be lost.  But similarly anything you broke developing or testing on it will also be magically returned to a pristine state ready to start again.  You can quickly see how this can become a powerful tool for testing and prototyping.

But often there will be more unique boxes to configure, or you may want a fast way to build a Vagrant box up to an appropriate state to do some testing. We can do this using some simple Puppet code. Vagrant supports using Puppet either in its solo mode (without a server) or in client-server mode. We’re going to use it in solo mode for this case. 

To get started create a directory called manifests in the vagrant home directory (/home/james/vagrant):

$ mkdir /home/james/vagrant/manifests

Then create a Puppet manifest in this directory, naming the manifest file with the name of the box we’re going to configure, in our case lucid32.pp. This is configurable in Vagrant but this is the common default. You could also use existing Puppet manifests if you have those -- a fast way of replicating a production host as a Vagrant virtual machine.

$ touch /home/james/vagrant/manifests/lucid32.pp

And add a simple manifest inside the file:

class lucid32 {
  package { "apache2":
    ensure => present,
  }

  service { "apache2":
    ensure => running,
    require => Package["apache2"],
  }
}

include lucid32

This manifest will install the Apache package and start the Apache service.

We now need to enable Puppet inside Vagrant’s configuration file, called Vagrantfile. This file will be in our /home/james/vagrant directory and we need to open it and edit the content to add the following lines:

Vagrant::Config.run do |config|
...
  # Enable the Puppet provisioner
  config.vm.provision :puppet
end

The key line being config.vm.provision :puppet. This tells Vagrant to use Puppet. It also installs Puppet on the base box you just downloaded - in this case it and many of the other base boxes available also have Puppet installed (you can create your own boxes).

Now we can bring our Vagrant box back up:

$ vagrant up

Or if the Vagrant box is already running then you can initiate provisioning like so:

$ vagrant reload

This will load and execute Puppet and the manifest file you have specified, and configure the box by installing Apache and starting the Apache service. Vagrant uses VirtualBox port forwarding to forward port 80 (HTTP) by default so you should be able to browse to that port on the local host and see the default Apache page. You can also configure additional ports to forward.

Now you have a fast, easy virtual environment to conduct whatever testing or prototyping required. There is more information on how to use Vagrant on the software's website.

This was first published in March 2011

Dig deeper on Linux virtualization

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataCenter

SearchServerVirtualization

SearchCloudComputing

SearchEnterpriseDesktop

Close