Create Puppet modules using Defined Types

Getting Started with Puppet Like Classes, Defined Type is also used to create Puppet modules. However, it is still different from Classes.

Defined Types are dynamic in nature. It means you can pass some arguments while calling Defined Types. The actual value will be replaced by the value passed in the argument.

Let’s try to understand with a real world example.  I need to add a new site in Apache, which means I need to create a new virtual host for that particular website. In order to make this process dynamic, I will have to create a new virtual host Config file every time a new site needs to be added. Rather than doing it manually all the time, I’ll be using Puppet to automate this process.

Here is the example which explains this process.

  1. Create a Defined Type, which accepts two run-time arguments:
    1. Name of the website/virtual host
    2. Content of the virtual host
  2. Call that Defined Type and pass the run-time values.

1. Define the “Defined Type”

 define virtual_host($content) { file { "/etc/apache2/sites-available/$name": ensure => 'file', owner => 'root', group => 'root', mode => '644', content =>$content; } }
2. Call the “Defined Type”
 virtual_host { 'example.net': content => file('/etc/puppet/modules/apache2/files/vhosts/example.net'); }
In the above example, the website/virtual host name is “example.net” and source for the content is a file which is place on the Puppet server itself (within the module).


Create Puppet modules using Classes

A Puppet module is a reusable unit of manifest code, similar to functions or methods in procedural languages. A Puppet module can be defined in either of the following ways:

  1. Using classes
  2. Using defined types

In this article, I’m going to explain how to create a Puppet module using Classes. A Puppet class can be considered to be a container for resources. It is defined once and selected by all nodes that need to make use of the prepared functionality. Each class represents a well-known subset of a system’s configuration.

Here is the syntax of the class.

 class name { type {‘name’: attribute => value, } type {‘name’: attribute => value, } }
First thing you need to do is to create directory structure for your module. It needs to be created under “modules” directory present in your Puppet home directory (on most of the systems, Puppet home directory is /etc/puppet)

Here are step by step instructions: Continue reading


How to define a Puppet Resource

A Puppet resource is a fundamental unit for modelling system configurations. Each resource describes some aspect of the system, e.g. Service, Package, File, etc. The block of Puppet code that describes a resource is called resource declaration.

Every resource has a type, title and set of attributes.


 type {“title”: property => value, }

 package {“apache2”: ensure => installed, }
Continue reading