Installing puppet on debian, part 2

This is the next part for the puppet on debian wheezy tutorial. In this part we move code from site.pp to a separate module which is the new method used for puppet 3.6 and up.

If you have a lot of manifest code for different servers your site.pp will become unmanageable. For this purpose Puppet created modules. Since puppet 3.6 you should not only put your downloaded modules but also your own reusable code in a module. A module is also the only place you can put templates without it throwing depricated warnings on every command.

Creating a module

Puppet has a build in command to create a new empty module. Run:

$ puppet module generate yourname-modulename

This command will create a new directory structure in /etc/puppet/modules/ with your selected name

modules
.
|-- manifests
| |- init.pp
|-- metadata.json
|-- Rakefile
|-- README.md
|-- spec
| |-- classes
| | |-- init_spec.rb
| |-- spec_helper.rb
|-- tests
|-- init.pp

Now for easy usage rename /etc/puppet/modules/yourname-modulename to /etc/puppet/modules/modulename

The main code for your module goes into manifests/init.pp. It's almost the same as site.pp in your main manifest directory. Copy the full basenode to init.pp and replace node basenode with class modulename (the class name in init.pp should match the module name).

The init.pp file should now contain something like:

class modulename {
include '::ntp'
class { 'vim':
opt_syntax => true,
opt_misc => ['expandtab', 'shiftwidth=4'],
}
class { '::ntp':
servers => [ 'nl.pool.ntp.org' ],
}

user { "martijn":
ensure => "present",
managehome => true,
}

package { 'htop': ensure => installed }
}

To use this module change your site.pp to:

# site.pp
node basenode {
include modulename
}

node 'server1.company.local' inherits basenode {
}

node 'server2.company.local' inherits basenode {
package { 'apache2': ensure => installed }
package { 'mysql-server': ensure => installed }
}

You can also remove the complete basenode and define the module in the server nodes:

# site.pp
node 'server1.company.local' {
include modulename
}

node 'server2.company.local' {
include modulename
package { 'apache2': ensure => installed }
package { 'mysql-server': ensure => installed }
}

Now one extra feature you get with modules is easy template usage. An example of template usage is the puppetlabs-motd module. This module creates a /etc/motd file that is shown every time you log in on your server. This file is created with a template file.

To use the module you first need to download it to the puppetmaster:

$ puppet module install puppetlabs-motd

For the basic usage you should include it in init.pp in your module. Add the following line to your modulename class:

include motd

If you run puppet agent --test now the /etc/motd file is overwritten with the one provided by puppetlabs. It contains your distro name and free memory. To change the the motd that is generate you need to create a template.

Create the file directory templates in your module directory and inside that the file motd.erb (erb files are ruby template files). The puppetlabs motd is found in /etc/puppet/modules/motd/templates/motd.erb.

After you've created and modified the motd.erb in your module you need to change the include motd line to set the new motd template. change the line into:

class { 'motd':
template => 'modulename/motd.erb',
}

If you run the puppet agent now (or wait 30 minutes) the /etc/motd is replaced by the one generated by your template.

This concludes this part in the puppet for debian tutorial.