Puppet: Hello Modules

We’re onto post 3 of our Puppet series. We have a master server, a client and we’ve made a connection. Its time to start getting into the real power of puppet and write our first module.

Module Make Up

So what makes up a module. They can be as simple as an init.pp file or as complex as having directories of files, templates and just about everything else under the sun there to help control your infrastructure. Typically, our modules are going to be under /etc/puppet/modules.

Before you go much further, I highly recommend you read Writing Great Modules: An Introduction by PuppetForge manager Ryan Coleman. A lot of good stuff in that article.

Now, onto the nitty gritty. The typical pattern that you should follow in your module is to define a class and then define the package, file, and service. This is the very bare minimum you should follow and will most likely work for most modules that you write for your own environment.

Let’s keep it simple – MOTD

For a very simple and basic example, let’s simply create a module that sets our MOTD to a standard message. This example is so simple that we don’t have to worry about package or service section of the module. Its simple updating a file on the system.

cd /etc/puppet/modules
mkdir -p motd/manifests
cd motd/manifests
vim init.pp

# In our init.pp, we're going to define a class of motd and define a file of how it should look
class motd
{
  file
  {
    '/etc/motd':
      ensure  => file,
      mode    => 644,
      content => "Welcome to ${::hostname} - ${::fqdn} - ${::operatingsystem} ${::operatingsystemrelease}
This node is under the control of Puppet ${::puppetversion}
Have a lot of fun...enjoy the fish
";
  }
}

This simple example does a nice little thing with our MOTD so when I log in I see:

 matt@puppet ~]$ ssh root@web1
 Enter passphrase for key '/home/matt/.ssh/id_rsa':
 Last login: Thu Sep  6 14:59:00 2012 from puppet.usrlocal.com
 Welcome to web1 - web1.usrlocal.com - CentOS 6.3
 This node is under the control of Puppet 2.6.17
 Have a lot of fun...enjoy the fish

Fancy right?

More hotness

So how can we up this a level. What about a simple template? Sure we can do that to and give us some additional information.

First, we need to create our template

 cd /etc/puppet/modules/motd/templates
 vim motd.erb

 
 Welcome to <%= hostname -%> - <%= fqdn -%> - <%= operatingsystem -%> <%= operatingsystemrelease %>
 This node is under the control of Puppet <%= puppetversion %>

 Kernel: <%= kernel -%> <%= kernelversion %>
 Ram: <%= memorysize %>
 Days Up: <%= uptime_days %>

 Have a lot of fun...enjoy the fish!

Next we need to change our init.pp file to use the template instead. We simply change our content portion to instead of having a string of text, to use the template that we just created.

 vim /etc/puppet/modules/motd/manifests/init.pp
 
 class motd
 {
  file
  {
    '/etc/motd':
      ensure  => file,
      mode    => 644,
      content => template("motd/motd.erb");
   }
 }

Now when we log in we get a bit more information based off the facts from the server itself.

 Last login: Thu Sep 27 15:32:25 2012 from puppet.usrlocal.com
 Welcome to web1 - web1.usrlocal.com - CentOS 6.3
 This node is under the control of Puppet 2.6.17
 
 Kernel: Linux 2.6.32
 Ram: 1.96 GB
 Days Up: 52
 
 Have a lot of fun...enjoy the fish!

Leveling up…

For those that are looking to up their game in module writing, I highly recommend the following:

Up next

Next we’ll discuss how to use the puppetforge to save yourself some time working off the work of others.