Build a Base Box for Vagrant and Puppet deployment

Introduction

If you want to create your own Vagrant Base Box using an Oracle Virtualbox as the vagrant provider, you can and it is fairly straight forward. I have detailed the steps in this post. I am using Windows 7 as my “host” operating system on my laptop and I am going to build a Centos 6 base box for my Vagrant template.

I am also going to use the popular configuration management tool puppet to deploy customisations to my Vagrant boxes. In this case I am going to complete a really simple change and add a user called “jenkins”

Let’s get started:

Ensure you have Virtual Box Installed

Vagrant alone doesn’t provide and virtualisation capabilities, it is simply a very useful tool for provisioning VM guests is a quick and standard manner.  We will need to provide some type of hypervisor to run our virtual guests. In this example we are going to use the Oracle Virtual Box 4.3.20.  It is free, so go ahead and install.

Ensure you have Vagrant installed

Install vagrant as normal on your host machine.

Then open up a cmd prompt. From within the command prompt type “vagrant”


vagrant

If vagrant is installed the command should return the following output (below) if it does then success you are ready to start with vagrant.

Usage: vagrant [-v] [-h] command []

-v, –version Print the version and exit.
-h, –help Print this help.

Available subcommands:
box manages boxes: installation, removal, etc.
destroy stops and deletes all traces of the vagrant machine
halt stops the vagrant machine
help shows the help for a subcommand
init initializes a new Vagrant environment by creating a Vagrantfi
e
package packages a running vagrant environment into a box
plugin manages plugins: install, uninstall, update, etc.
provision provisions the vagrant machine
reload restarts vagrant machine, loads new Vagrantfile configuration
resume resume a suspended vagrant machine
ssh connects to machine via SSH
ssh-config outputs OpenSSH valid configuration to connect to the machine
status outputs status of the vagrant machine
suspend suspends the machine

Create the base image in Oracle VirtualBox

Create an image you want to use as the base box. This is the “template” guest machine which will be used to build all your servers with vagrant.   In my example I am using a Centos 6.5 minimal guest. I have highlighted it in the VirtualBox manager below:

VirtualBoxBase

 

Install Guest Additions

This is an important part of the process if you are planning to use Puppet to provision  your build which I am.  This provides the shared folders capability for accessing the puppet configuration on the host operating system which we will cover later in this guide.

Once your machine is built then ensure the guest additions are installed.  There is a useful guide to this here these are the steps I followed

Ensure the Virtual Machine Additions install CD is mounted.

  • Select your virtual machine, select show. This will bring up a Virtual machine “console”
  • Choose Devices and “Insert Guest Additions CD image”
adding_virtual_additions
Mount the cdrom
We have now “inserted” the cdrom into our virtual guest we need to mount it to the file system so we can access the installer routine

mount /dev/cdrom /mnt

Add the Prequisites for CentOS 6

I did this as the root user from the root home dir /root

Add the RPM Key

root@192.168.1.117's password:
Last login: Sat Jan 3 08:25:43 2015
[root@localhost ~]# rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
[root@localhost ~]#

Download the rpm

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

Install the rpm
rpm -i rpmforge-release-0.5.3-1.el6.rf.*.rpm

Install DKMS
yum --enablerepo rpmforge install dkms

Install the Development Tools
yum groupinstall "Development Tools"
yum install kernel-devel
yum install kernel-devel-2.6.32-504.el6.x86_64

Run the Installer

cd /mnt
sh VBoxLinuxAdditions.run

This will install and return output like so


Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.20 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.20 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules [ OK ]
Removing existing VirtualBox non-DKMS kernel modules [ OK ]
Building the VirtualBox Guest Additions kernel modules [ OK ]
Doing non-kernel setup of the Guest Additions [ OK ]
Starting the VirtualBox Guest Additions [ OK ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.

Add a Vagrant User

We use this user to do our build tasks, rather than root.

Add the user

useradd vagrant

Set the password to vagrant

passwd vagrant

Grant the vagrant user full rights with sudoers


echo vagrant ALL=NOPASSWD: ALL >> /etc/sudoers.d/vagrant

Add the Vagrant Guest SSH Key

The vagrant user will use public key authentication to access the guest whilst provisioning.  There is a standard key and it can be found here.

Create a .ssh directory for the vagrant user and set correct permissions

mkdir /home/vagrant/.ssh
chmod 700 /home/vagrant/.ssh
chown vagrant /home/vagrant/.ssh


sudo su - vagrant


curl -k https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub > .ssh/authorized_keys

Remove the TTY Requirement

Using visudo comment out the Defaults requiretty entry.

Convert this VM Guest to a Vagrant “box”

We are going to convert the vm we just created “Centos6-Base” to a vagrant box and store this box ready to be used to fire up development environments. I will keep it under a folder c:\myvagrant\boxes\

Create this box storage folder , then from the command prompt

vagrant package --base Centos6-Base --output C:\myvagrant\boxes\Centos6-Base.box

which will output something like the following:
vagrant_package

Prepare the Vagrant default file

Now you have a a vagrant box to use as a template we can set about creating our vagrant file.

From the c:\myvagrant folder, type the command

vagrant init

This will initialize the directory and create a default vagrant file.

c:\myvagrant\vagrant

Set Up Puppet

Tp have pupet finish off your vagrant builds we need to create some puppet configuration. So lets do that.

Create a manifests folder

We are going to create a folder where we can hold our puppet configuration.  So to do that we need to create a folder under the c:\myvagrant folder called manifests.  Go ahead and do that.

Setup a Puppet Manifest File “default.pp”

In this example we are going to use puppet to add a new user to our vm.  This user will be named jenkins and will have a home directory of /home/jenkins.  Pretty standard stuff but enough to illustrate how to use puppet on a vagrant deployment.

Under the manifests folder, create a file called default.pp and enter the following puppet directives

user {'jenkins':
name => 'jenkins',
comment => 'Jenkins Service Account',
managehome => true,
}

package {'java-1.7.0-openjdk-devel':
ensure => "installed"
}

now you we have a vagrant layout that resembles something like this:

vagrant_file_system

 

 

Nearly There: Create our vagrant file

The default vagrant file needs to be replaced with our own one.  So open the c:\vagrant\vagrant file and replace the text within with the following text:


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

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

Vagrant.configure("2") do |config|
# tell vagrant which base box we want to use (the one we just created)
config.vm.box = "C:/vagrant/boxes/Centos6-Base.box"

#Set a custom O/S hostname (not the default localhost)
config.vm.hostname = "centosclean"

#Set some virtual settings
config.vm.provider "virtualbox" do |v|
v.cpus = 2
v.gui = true
v.memory = 1024
v.name = "centosclean"
end
#Install Puppet

$script = <