dracoblue.net

Test a Puppet Host with Vagrant

In the previous post we had a look on configuring the puppet config for a single machine with git.

This looks quite good, but before we want to roll out the config to our target machine, we might test it with a vagrant machine first.

Install vagrant (and virtualbox) before we can continue.

Since the puppet version on vagrant boxes can become outdated, I use this small script to ensure that a specific puppet version is installed (e.g. 3.4.0 in this example):

#!/bin/bash

PUPPET_VERSION=3.4.0

# update apt-get cache if older than 60 minutes
test `find /var/cache/apt/pkgcache.bin -cmin +60` && apt-get update --fix-missing

if [ ! `puppet --version | grep "$PUPPET_VERSION"` ]
then
    apt-get install --yes lsb-release
    DISTRIB_CODENAME=$(lsb_release --codename --short)
    DEB="puppetlabs-release-${DISTRIB_CODENAME}.deb"
    DEB_PROVIDES="/etc/apt/sources.list.d/puppetlabs.list" # Assume that this file's existence means we have the Puppet Labs repo added

    if [ ! -e $DEB_PROVIDES ]
    then
        # Print statement useful for debugging, but automated runs of this will interpret any output as an error
        # print "Could not find $DEB_PROVIDES - fetching and installing $DEB"
        wget -q http://apt.puppetlabs.com/$DEB
        sudo dpkg -i $DEB
        sudo apt-get update
    fi
    sudo apt-get install --yes puppet=${PUPPET_VERSION}* puppet-common=${PUPPET_VERSION}*
fi

Save this file as ensure_puppt_version.sh into your puppet git repository. This script has also another little gem in the 5th line, which will update apt-get if the cache is older then 60 minutes. The script is inspired from (this blogpost.

Now create a Vagrantfile which might look like this:

# -*- 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(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "precise64"
  config.vm.box_url = " http://files.vagrantup.com/precise64.box"

  config.vm.network :private_network, ip: "10.10.10.10"
  config.ssh.forward_agent = true

  config.vm.hostname = "vagrant-hans.local"

  config.vm.provider :virtualbox do |vb|
     # 1024 MB ram is way better
     vb.customize ["modifyvm", :id, "--memory", "1024"]
     # 2 cpus is better, too
     vb.customize ["modifyvm", :id, "--cpus", "2"]
     vb.customize ["modifyvm", :id, "--ioapic", "on"]
  end

  config.vm.provision :shell, :path => "ensure_puppet_version.sh"

  config.vm.provision :puppet do |puppet|
     puppet.manifests_path = "manifests"
     puppet.manifest_file  = "default.pp"
     puppet.module_path = "modules"
  end

end

At the beginning I set the ip on something (e.g. 10.10.10.10), which I can remember, so that I can access the box's webserver easily afterwards.

I use vagrant-hans.local as hostname here. This will be useful if we are using the $fqdn within the puppet files in later blog posts. Also it will make that the shell on the machine looks like this (so you know on which machine you are):

user@vagrant-hans:/var/log$

Also I ensure that memory is 1024MB and cpus are 2, since the defaults might not be sufficient for my tests.

Finally launch the box:

vagrant up

Now you can update your manifests/default.pp in any way you want and test your puppet files with:

vagrant provison

Now let's install some modules. I also wrote a guide, if you don't want to commit your puppet modules, but want to update the puppet modules them on demand.

In this post we asume, that you download the modules from puppetforge, github or any other source and commit them to your repository. Let's say we download the stdlib module from puppetforge.

Create a new folder in your modules folder, called: available. Go to https://forge.puppetlabs.com/puppetlabs/stdlib and press the Or: Download as a .tar.gz button.

Extract it into the available folder, It will look like this:

manifests/
  default.pp
modules/
  available/
    puppetlabs-stdlib-4.1.0

We created this extra avaible folder to enable only those modules for puppet, which are wanted. We do this with symlinks:

$ cd modules
$ ln -s puppetlabs-stdlib-4.1.0 stdlib

So now the folder structure looks like this:

manifests/
  default.pp
modules/
  stdlib -> symlink to available/puppetlabs-stdlib-4.1.0
  available/
    puppetlabs-stdlib-4.1.0

Now you can easily download multiple versions of modules (or update them in the future) and see with one quick look, which version of the module is installed.

If you commit the Vagrantfile, modules and everything else to your box git repository, you can maintain the configuration for your test environment and live environment all at one place!

In open source, puppet, ubuntu, vagrant by
@ 03 Feb 2014, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About