Date Tags devops
Automate all the things

With a superb buzzword laden title like that, then I reckon massive traffic boost is inevitable.

Puppet is my favourite Configuration Management tool. This is not a post to try and persuade anyone not to use Ansible, Chef or any other. What I want to do is show I build Puppet based infrastuctures in such away that it meets all the basic tenets of DevOps/Agile/buzzword-of-the-month.

What to we need:

  • CentOS 6 - RHEL/CentOS is pretty much the defacto enterprise distro. This will easily translate to Debian/Ubuntu or anything else.
  • Puppet 3 - I like a traditional Master/Agent set up, if you prefer master-less good for you. This is my blog, my rules.
  • Git
  • Dynamic Environments
  • PuppetDB
  • Hiera
  • Jenkins

All the config is stored in Git, with Jenkins watching it.

Puppet tends to fall apart pretty quickly if you do not have DNS in place. You can start using host files, but that will get old quickly. Ideally, the first thing you will do with Puppet is install a DNS server managed by Puppet. Maybe that will be the next post.


Starting with a base Centos 6 install, the installation is very easy:

yum -y install
yum -y install puppet puppet-server rubygem-activerecord

Our environments need a place to go, so create that:

mkdir /etc/puppet/environments
chgrp puppet /etc/puppet/environments
chmod 2775 /etc/puppet/environments

The configuration will look like:

    logdir = /var/log/puppet
    rundir = /var/run/puppet
    ssldir = $vardir/ssl
    trusted_node_data = true
    pluginsync = true

    classfile = $vardir/classes.txt
    localconfig = $vardir/localconfig
    report = true
    environment = production
    ca_server = puppet.chriscowley.lan
    server = puppet.chriscowley.lan

    environmentpath = $confdir/environments
    # Passenger
    ssl_client_header        = SSL_CLIENT_S_DN
    ssl_client_verify_header = SSL_CLIENT_VERIFY

Do not use the Puppetmaster service. It uses Webrick, which is bad. Any more than 5 agents and it will start slowing down. Puppet is a RoR app, so stick it behind Apache/Passenger. We installed the puppet-server package for a simple reason: when you start it the first time, it will create your SSL certificates automatically. After that initial start you can stop it and forget it ever existed. So just run:

service puppetmaster start
service puppetmaster stop

Unfortunately, you will need to put SELinux into Permissive mode temporarily. Once you have fired it up you can build a local policy and re-enable it.

yum install httpd httpd-devel mod_ssl ruby-devel rubygems gcc gcc-c++ curl-devel openssl-devel zlib-devel
gem install rack passenger

Next you need to configure Apache to serve up the RoR app.

mkdir -p /usr/share/puppet/rack/puppetmasterd
mkdir /usr/share/puppet/rack/puppetmasterd/public /usr/share/puppet/rack/puppetmasterd/tmp
cp /usr/share/puppet/ext/rack/ /usr/share/puppet/rack/puppetmasterd/
chown puppet:puppet /usr/share/puppet/rack/puppetmasterd/ | sed 's/'

You will need to modify the sed command in the last line to match your environment.

You may also need to change the Passenger paths to match what the output of passenger-install-apache2-module told you. It is up to date as of the time of writing.


Your config file (/etc/puppet/hiera.yaml) will already be created, mine looks like this:

  - yaml
  - defaults
  - "nodes/%{clientcert}"
  - "virtual/%{::virtual}"
  - "%{environment}"
  - "%{::osfamily}"
  - global

  :datadir: "/etc/puppet/environments/%{::environment}/hieradata"

There is also an /etc/hiera.yaml which Puppet does not use. change this to a symbolic link to avoid confusion.

ln -svf /etc/puppet/hiera.yaml /etc/hiera.yaml

If you were to test it now, you will see a few errors:

Info: Retrieving pluginfacts
Error: /File[/var/lib/puppet/facts.d]: Could not evaluate: Could not retrieve information from environment production source(s) puppet://puppet/pluginfacts
Info: Retrieving plugin
Error: /File[/var/lib/puppet/lib]: Could not evaluate: Could not retrieve information from environment production source(s) puppet://puppet/plugins

Don't worry about that for now, the important thing is that the agent connects to the master. If that happens the master does return an HTTP error, then you are good.


This is the tool I use to manage my modules. It can pull them off the Forge, or from wherever you tell it too. Most often that will be Github, or an internal Git repo if that's what you use.

You need to install it from Ruby Gems, then there is a little configuration to do.

gem install r10k
mkdir /var/cache/r10k
chgrp puppet /var/cache/r10k
chmod 2775 /var/cache/r10k

The file /etc/r10k.yaml should contain:

# location for cached repos
:cachedir: '/var/cache/r10k'

# git repositories containing environments
    remote: '/srv/puppet.git'
    basedir: '/etc/puppet/environments'

# purge non-existing environments found here
  - '/etc/puppet/environments'


The core of your this process is the ubiquitous Git.

yum install git

You need a Git repo to store everything, and also launch a deploy script when you push to it. To start with we'll put it on the Puppet server. In the future I would put this on a dedicated machine, have Jenkins run tests, then run the deploy script on success.

However, it is not a standard repository, so you cannot just run git init. It needs:

  • To be bare
  • To be shared
  • Have the master branch renamed to production
mkdir -pv /srv/puppet.git
git init --bare --shared=group /srv/puppet.git
chgrp -R puppet /srv/puppet.git
cd /srv/puppet.git
git symbolic-ref HEAD refs/heads/production

Continuing to work as root is not acceptable, so create user (if you do not already have one).

useradd <username>
usermod -G wheel,puppet <username>

Uncomment the line that reads:

%wheel        ALL=(ALL)       ALL

This gives your user full sudo privileges.

Deploy script

This is what does the magic stuff. It needs to be /srv/puppet.git/hooks/post-receive so that it runs when you push something to this repository.


umask 0002

while read oldrev newrev ref
    branch=$(echo $ref | cut -d/ -f3)
    echo "--> Deploying ${branch}..."
    r10k deploy environment $branch -p
    # sometimes r10k gets permissions wrong too
    find /etc/puppet/environments/$branch/modules -type d -exec chmod 2775 {} \; 2> /dev/null
    find /etc/puppet/environments/$branch/modules -type f -exec chmod 664 {} \; 2> /dev/null

Run chmod 0775 /srv/puppet.git/hooks/post-receive to make is executable and writable by anyone in the puppet group.

The first environment

Switch to your user

su - <username>

Clone the repository and create the necessary folder structure:

git clone /srv/puppet.git
cd puppet
mkdir -pv hieradata/nodes manifests site

Now you can create PuppetFile in the root of that repository. This is what tells R10k what modules to deploy.

# Puppet Forge
mod 'puppetlabs/ntp', '3.0.0-rc1'
mod 'puppetlabs/puppetdb', '3.0.1'
mod 'puppetlabs/stdlib', '3.2.1'
mod 'puppetlabs/concat', '1.0.0'
mod 'puppetlabs/inifile', '1.0.3'
mod 'puppetlabs/postgresql', '3.3.3'
mod 'puppetlabs/firewall', '1.0.2'
mod 'chriscowley/yumrepos', '0.0.2'

# Get a module from Github
#mod 'custom',
#  :git => '',
#  :ref => 'master'

A common error I make if I am not looking properly is to put the SSH URL from Github in there. This will not work unless you have added your SSH key on the Puppet server. Better just to put the HTTPS URL in there, there is need to write back to it after all.

Next you need to tell Puppet what agents should get what. To begin with, everything will get NTP, but only the Puppetmaster will get PuppetDB. To that end create hieradata/common.yaml with this:

  - ntp


Next create hieradata/nodes/$(hostname -s).yaml with:

  - puppetdb
  - puppetdb::master::config

Finally, you need to tell Puppet to get the data from Hiera. Create with


You should need nothing else.

Now you can push it to the master repository.

git add .
git commit -a -m "Initial commit"
git branch -m production
git push origin production


Of course, the whole point of all this is that we do as much testing as we can before any sort of deploy. We also want to keep our Git repository nice clean (especially if you push it to Github), so if we can avoid commits with stupid errors that would be great.

To perform your testing you need to replicate your production environment. From now on, I'm going to assume that you are working on your own workstation.

Clone your repository:

git clone ssh://<username>
cd puppet

To perform all the testing, RVM is your friend. This allows you to replicate the ruby environment on the master, have all the necessary gems installed in a contained environment and sets you up to integrate with Jenkins later. Install is with:

curl -sSL | bash -s stable

Follow any instructions it gives your, then you can create your environment. This will be using a old version of ruby as we are running CentOS 6 on the master.

rvm install ruby-1.8.7
rvm use ruby-1.8.7
rvm gemset create puppet
rvm gemset use puppet
rvm --create use ruby-1.8.7-head@puppet --rvmrc

Create a Gemfile that contains:

source ''

gem 'puppet-lint', '0.3.2'
gem 'puppet', '3.6.2'
gem 'kwalify', '0.7.2'

Now you can install the gems with bundle install.

The tests will be run by a pre-commit hook script, that looks something like:

# pre-commit git hook to check the validity of a puppet main manifest
# Prerequisites:
# gem install puppet-lint puppet
# Install:
# /path/to/repo/.git/hooks/pre-commit
# Authors:
# Chris Cowley <>

echo "Checking style"
for file in `git diff --name-only --cached | grep -E '\.(pp)'`; do
  puppet-lint ${file}
  if [ $? -ne 0 ]; then
    echo "Style looks good"

echo "Checking syntax"
for file in `git diff --name-only --cached | grep -E '\.(pp)'`; do
  puppet parser validate $file
  if [ $? -ne 0 ]; then
    echo "Syntax error in ${file}"
    echo "Syntax looks good"

for file in `git diff --name-only --cached | grep -E '\.(yaml)'`; do
  echo "Checking YAML is valid"
  ruby -e "require 'yaml'; YAML.parse('$file'))"
  if [ $? -ne 0 ]; then
    echo "YAML looks good"

if [ ${yaml_bad}  ];then
  exit 1
elif [ ${syntax_bad}  ]; then
  exit 1
elif [ ${style_bad}  ]; then
  exit 1
  exit 0

This should set you up very nicely. Your environments are completely dynamic, you have a framework in place for testing.

For now the deployment is with a hook script, but that is not the ultimate goal. This Git repo needs to be on the Puppet master. You may well already have a Git server you want to use. TO this end, in a later post I will be add Jenkins into the mix. As you are running the tests in an RVM environment, it will be very easy to put into Jenkins. This can then perform the deployment.


comments powered by Disqus