Om Scratch
Om Scratch
no
http://stdout.no/a-modern-puppet-master-from-scratch/
Install Puppet
Add the Puppet Labs repository by installing their handy package:
source /etc/lsb-release
wget https://apt.puppetlabs.com/puppetlabs-release-$DISTRIB_CODENAME.deb
dpkg -i puppetlabs-release-$DISTRIB_CODENAME.deb
rm puppetlabs-release-$DISTRIB_CODENAME.deb
Install Puppet and Puppet Master:
apt-get update
apt-get install puppet puppetmaster
Configure Puppet
Create a folder to hold our environments, and use the puppet group to manage write access to it:
mkdir /etc/puppet/environments
chgrp puppet /etc/puppet/environments
chmod 2775 /etc/puppet/environments
We will never modify the contents of this directory directly, but r10k will use it through a Git hook we set up later
on.
Now make some adjustments to /etc/puppet/puppet.conf. Here is a good starting point:
[main]
environment
confdir
logdir
vardir
ssldir
rundir
factpath
pluginsync
=
=
=
=
=
=
=
=
[agent]
environment
report
show_diff
= production
= true
= true
production
/etc/puppet
/var/log/puppet
/var/lib/puppet
$vardir/ssl
/var/run/puppet
$vardir/lib/facter
true
[master]
environment
= production
environmentpath = $confdir/environments
# Passenger
ssl_client_header
= SSL_CLIENT_S_DN
ssl_client_verify_header = SSL_CLIENT_VERIFY
If you don't have DNS setup so that looking up "puppet" resolves to your server, then you can also work around
that here by adding server = your.server.com to the [main] section.
Configure Hiera
Hiera needs some setup as well. Create the file /etc/puppet/hiera.yaml:
--:hierarchy:
- "nodes/%{::fqdn}"
- "manufacturers/%{::manufacturer}"
- "virtual/%{::virtual}"
- common
:backends:
- yaml
:yaml:
:datadir: "/etc/puppet/environments/%{::environment}/hieradata"
In order to make debugging Hiera lookups (slightly) easier and avoid confusion later, I also like to replace
/etc/hiera.yaml (which Puppet doesn't care about) with a link to /etc/puppet/hiera.yaml:
ln -sf /etc/puppet/hiera.yaml /etc/hiera.yaml
Test Puppet
This would be a good time to restart the Puppet Master service:
/etc/init.d/puppetmaster restart
Test that the Puppet agent works as expected:
puppet agent --test
The output at this point should be along these lines:
Info: Retrieving plugin
Error: /File[/var/lib/puppet/lib]: Could not evaluate: Could not retrieve
information from environment production source(s) puppet://testpm.qix.no/plugins
Info: Caching catalog for testpm.qix.no
Info: Applying configuration version '1384949455'
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.03 seconds
The single error message can be ignored, because we have no config or plugins yet.
Make sure this works before continuing. Problems here are usually DNS related.
Install r10k
The excellent Adrien Thebo has created an equally excellent tool for managing dynamic Puppet environments
and using external modules efficiently, whether you find them on the Puppet Forge or keep them in your own
repositories.
More information can be found on GitHub. Install it like so:
# note: rubygems package not needed on Ubuntu 14.04
apt-get install rubygems
gem install r10k
Configure r10k
The cache directory where r10k stores copies of modules has to be created:
mkdir /var/cache/r10k
chgrp puppet /var/cache/r10k
chmod 2775 /var/cache/r10k
And finally r10k has its own config file. Create /etc/r10k.yaml with the following content:
# location for cached repos
:cachedir: '/var/cache/r10k'
# git repositories containing environments
:sources:
:base:
remote: '/srv/puppet.git'
basedir: '/etc/puppet/environments'
# purge non-existing environments found here
:purgedirs:
- '/etc/puppet/environments'
Install Git
The version of Git that comes with Ubuntu 12.04 unfortunately still suffers from this bug, which sets the wrong
mode (0755) on all new Puppet environments. This breaks sharing of repos among multiple users.
If you're on 12.04 (or you just want the latest Git), add the PPA for Ubuntu Git Maintainers team:
apt-get install python-software-properties
add-apt-repository ppa:git-core/ppa
Install the latest stable Git:
apt-get update
apt-get install git
- 1.pool.ntp.org
- 2.pool.ntp.org
- 3.pool.ntp.org
This Puppet Master needs the puppetdb module, so create hieradata/nodes/$(hostname -f).yaml and
include the class there, along with the recommended basic configuration:
--classes:
- puppetdb
- puppetdb::master::config
Finally create a very simple manifests/site.pp that includes any classes we define in Hiera:
hiera_include('classes')
checkout -b production
add *
commit -a -m "initital commit"
push -u origin production
Run Puppet
Back to root again, and run the Puppet agent:
puppet agent --test
You should get several screens of output in all grey and green showing the installation and configuration of the
Test PuppetDB
Run Puppet one more time to get some data in the database:
puppet agent --test
Now run the following:
puppet node status $(hostname -f)
This should give you output like:
testpm.qix.no
Currently active
Last catalog: 2013-11-20T13:22:05.036Z
Last facts: 2013-11-20T13:22:00.437Z
Bonus tip: Try the following to see everything PuppetDB has stored about your host, in pretty-printed JSON:
puppet node find $(hostname -f) | python -mjson.tool
Your PuppetDB is now operational, and you can use exported resources to do tricks like share ssh host keys
across hosts.
Test Hiera
If you got this far then Hiera is already working, but you may want to test Hiera from the command line during
development.
Assuming you followed my advice about linking /etc/hiera.yaml to /etc/puppet/hiera.yaml then the
following command will list all classes that will be applied to the current host with the production environment:
hiera -a classes ::environment=production ::fqdn=$(hostname -f)
This should return:
["puppetdb", "puppetdb::master::config", "ntp"]
deploy
# update all modules in testing environment
deploy testing
Once you're done with your module, make sure to tag it on your git server:
git tag -a 1.0 -m "finally no error messages"
git push --tags
...and update your Puppetfile to reference the module by tag rather than branch. You will thank yourself later.