Puppet Modules Use for IT Infrastructure Automation

Puppet Introduction:-

Puppet is one of the popularly used DevOps tools that is widely used for configuration management. It is used to bring about consistency in the Infrastructure. Puppet can define infrastructure as code, manage multiple servers, and enforce system configuration, thus helping in automating the process of infrastructure management.

Puppet has its own configuration language, Puppet DSL. As with other DevOps programs, Puppet automates changes, eliminating manual script-driven changes. However, Puppet is not simply another shell language, nor is it a pure programming language, such as PHP. Instead, Puppet uses a declarative model-based approach to IT automation. This enables Puppet to define infrastructure as code and enforce system configuration with programs.

What are the Key terms in Puppet Programming?

1. Manifests

A puppet program is called manifest and has a filename with .pp extension. Puppet’s default main manifest is /etc/puppet/manifests/site.pp. (This defines global system configurations, such as LDAP configuration, DNS servers, or other configurations that apply to every node).

2. Classes

Within these manifests are code blocks called classes other modules can call. Classes configure large or medium-sized chunks of functionality, such as all the packages, configuration files, and services needed to run an application. Classes make it easier to reuse Puppet code and improve readability.

3. Resources

Puppet code is made up mostly of resource declarations. A resource describes a specific element about the system’s desired state. For example, it can include that a specific file should exist or a package should be installed.

4. Puppet Modules

Except for the main site.pp manifest, it stores manifests in modules.

All of our Puppet code is organized in modules which are the basic building blocks of puppet that we can reuse and share. Each module manages a specific task in the infrastructure, such as installing and configuring a piece of software. 

Modules contain Puppet classes, defined types, tasks, task plans, capacities, resource types, and plugins, for example, custom types or facts. Install modules in the Puppet module-path. Puppet loads all content from every module in the module-path, making this code available for use.

Puppet Program Workflow

We will use Puppet’s declarative language to describe the desired state of the system in files called manifests. Manifests describe how you should configure your network and operating system resources, such as files, packages, and services.

Puppet compiles manifests into catalogs and it applies each catalog to its corresponding node to ensure that configuration of the node is correct across your infrastructure.

How to create a module from scratch?

In this puppet module, we will deal with tasks such as downloading the Apache package, configuring files, and setting up virtual hosts.

  • From the Puppet Master, navigate to Puppet’s module directory and create the Apache directory:12cd /etc/puppet/modulessudo mkdir apache
  • From inside the apache directory, create subdirectories: manifests, templates, files, and examples.12cd apachesudo mkdir {manifests, templates, files, examples}
  • Navigate to the manifests directory:1cd manifests
  • From here, we will separate the module into classes based on the goals of that section of code. 

init.pp         ->  to download the Apache package

params.pp  ->  to define any variables and parameters

config.pp    -> to manage any configuration files for the Apache service.


1234567891011file { 'configuration-file':  path    =>$conffile,  ensure  =>file,  source  =>$confsource,  notify  => Service['apache-service'],} service { 'apache-service':  name          =>$apachename,  hasrestart    => true,}

service resource uses the already-created parameter that defined the Apache name on Red Hat and Debian systems. 
hasrestart attribute is used to trigger a restart of the defined service.

Step 3: Create the Virtual host files

Depending on your system’s distribution the virtual host’s files will be managed differently. Because of this, we will encase the code for virtual hosts in an if statement, similar to the one used in the params.pp class but containing actual Puppet resources.

  • From within the apache/manifests/ directory, create and open a vhosts.pp file. Add the skeleton of the if statement:


1234567class apache::vhosts { if $::osfamily == 'RedHat' {  }  elsif $::osfamily == 'Debian' {  }  else {}}

The location of the virtual host file on our CentOS 7 server is /etc/httpd/conf.d/vhost.conf. You need to create the file as a template on the Puppet master. Do the same for the Ubuntu virtual hosts file, which is located at /etc/apache2/sites-available/example.com.conf, replacing example.com with the server’s FQDN.

  • Navigate to the templates file within the apache module and then create two files for your virtual hosts:

For Red Hat systems:

12345678<VirtualHost *:80>    ServerAdmin <%= @adminemail %>    ServerName <%= @servername %>    ServerAlias www.<%= @servername %>    DocumentRoot /var/www/<%= @servername -%>/public_html/    ErrorLog /var/www/<%- @servername -%>/logs/error.log    CustomLog /var/www/<%= @servername -%>/logs/access.log combined</Virtual Host>

For Debian systems:

1234567891011<VirtualHost *:80>    ServerAdmin <%= @adminemail %>    ServerName <%= @servername %>    ServerAlias www.<%= @servername %>    DocumentRoot /var/www/html/<%= @servername -%>/public_html/    ErrorLog /var/www/html/<%- @servername -%>/logs/error.log    CustomLog /var/www/html/<%= @servername -%>/logs/access.log combined    <Directory /var/www/html/<%= @servername -%>/public_html>        Require all granted    </Directory></Virtual Host>

We use only two variables in these files: adminemail and servername. We will define these on a node-by-node basis, within the site.pp file.

  • Return to the vhosts.pp file. The templates created can now be referenced in the code:


1234567891011121314151617class apache::vhosts {   if $::osfamily == 'RedHat' {    file { '/etc/httpd/conf.d/vhost.conf':      ensure    => file,      content   => template('apache/vhosts-rh.conf.erb'),    }  } elsif $::osfamily == 'Debian' {    file { "/etc/apache2/sites-available/$servername.conf":      ensure  => file,      content  => template('apache/vhosts-deb.conf.erb'),    }  } else {      fail('This is not a supported distro.')  } }

Both distribution families call to the file resource and take on the title of the virtual host’s location on the respective distribution. For Debian, this once more means referencing the $servername value. The content attribute calls the respective templates.

  • Both virtual host files reference two directories. They are not on the systems by default. We can create these through the use of the file resource, each within the if statement. The complete vhosts.conf file should resemble:


123456789101112131415161718192021222324252627class apache::vhosts {   if $::osfamily == 'RedHat' {    file { '/etc/httpd/conf.d/vhost.conf':      ensure    => file,      content   => template('apache/vhosts-rh.conf.erb'),    }    file { [ '/var/www/$servername',             '/var/www/$servername/public_html',             '/var/www/$servername/log', ]:      ensure    => directory,    }  } elsif $::osfamily == 'Debian' {    file { "/etc/apache2/sites-available/$servername.conf":      ensure  => file,      content  => template('apache/vhosts-deb.conf.erb'),    }    file { [ '/var/www/$servername',             '/var/www/$servername/public_html',             '/var/www/$servername/logs', ]:      ensure    => directory,    }  } else {    fail ( 'This is not a supported distro.')  } }

Step 4: Test the module

  • Navigate to the apache/manifests/ directory, run the puppet parser on all files to ensure the Puppet coding is without error:

sudo /opt/puppetlabs/bin/puppet parser validate init.pp params.pp vhosts.pp

  • Navigate to the examples directory within the apache module. Create an init.pp file and include the created classes. Replace the values for $servername and $adminemail with your own:


12345serveremail = 'webmaster@example.com'$servername = 'puppet.example.com' include apacheinclude apache::vhosts
  • Test the module by running puppet apply with the –noop tag:
    sudo /opt/puppetlabs/bin/puppet apply --noop init.pp

vhosts.pp   ->  to define the virtual hosts.

This module will also make use of Hiera (a built-in key-value configuration data lookup system, used for separating data from Puppet code) data, to store variables for each node.

Step 1: Downloading Apache Package

Create init.pp class

Now we will create a init.pp file under manifests directory to hold the apache package.
As we have 2 different OS (ubuntu and CentOS7)  that use different package names for Apache, we will have to use a variable $apachename.


123456class apache {package { 'apache':name    => $apachename,ensure  => present,}}

package resource allows for the management of a package. This is used to add, remove, or ensure a package is present. 

In most cases, the name of the resource (apache, above) should be the name of the package being managed. Because of the different naming conventions, we call the actual name of the package upon with the name reference. Soname, calls for the yet-undefined variable $apachename

ensure reference ensures that the package is present.

Create params.pp file

The params.pp file will define the needed variables. While we could define these variables within the init.pp file, since more variables will need to be used outside of the resource type itself, using a params.pp file allows for variables to be defined in if statements and used across multiple classes.

Create a params.pp file and the following code.


123456789101112class apache::params {  if $::osfamily == 'RedHat' {  $apachename = 'httpd' } elsif $::osfamily == 'Debian' {  $apachename = 'apache2' } else {  fail('this is not a supported distro.') }}

Outside of the original init.pp class, each class name needs to branch off from apache. We call this class as apache::params. The name after the double colon should share a name with the file. An if statement is used to define the parameters, pulling from information provided by Facter, Puppet has facter installation as a part of its installation itself. Here, Facter will pull down the operating system family (osfamily), to discern if it is Red Hat or Debian-based.

With the parameters finally defined, we need to call the params.pp file and the parameters into init.pp. To do this, we need to add the parameters after the class name, but before the opening curly bracket ( { ). 

So the init.pp that we created earlier should look something like this:

1234567class apache ( $apachename = $::apache::params::apachename,) inherits ::apache::params {  package { 'apache':    name => $apachename,    ensure => present,  }}

The value string $::apache::params::value tells Puppet to pull the values from the apache modules, params class, followed by the parameter name. The fragment inherits ::apache::params allows for init.pp to inherit these values.

Step 2: Manage Configuration Files

The Apache configuration file will be different depending on whether you are working on a Red Hat- or Debian-based system. 

You can find the following dependency files at the end of this demo: httpd.conf (Red Hat), apache2.conf (Debian).

  • Copy the content of httpd.conf and apache2.conf in separate files and save them in the files directory at /etc/puppetlabs/code/environments/production/modules/apache/files.
  • Edit both the files to disable keepalive. You will need to add the line KeepAlive Off in the httpd.conf file. If you do not want to change this setting, we should add a comment to the top of each file:
1#This file is managed by puppet

Add these files to the init.pp file, so Puppet will know the location of these files on both the master server and agent nodes. To do this, we use the file resource.


12345file { 'configuration-file':  path    =>$conffile,  ensure  =>file,  source  =>$confsource,}

Because we have the configuration files in two different locations, we give the resource the generic name configuration-file with the file path defined as a parameter with the path attribute. 

ensure ensures that it is a file. 

source provides the location on the Puppet master of the files created above.

Open the params.pp file. 

We define the $conffile and $confsource variables within the if statement:


1234567891011121314151617if $::osfamily == 'RedHat' { ...   $conffile     = '/etc/httpd/conf/httpd.conf'  $confsource   = 'puppet:///modules/apache/httpd.conf'}elsif $::osfamily == 'Debian' { ...   $conffile     = '/etc/apache2/apache2.conf'  $confsource   = 'puppet:///modules/apache/apache2.conf'}else { ...

We need to add the parameters to the beginning of the apache class declaration in the init.pp file

It should return no errors and output that it will trigger refreshes from events. To install and configure apache on the Puppet master, run again without –noop, if so desired.

  • Navigate back to the main Puppet directory and then to the manifests folder (not the one present in the Apache module).

cd /etc/puppetlabs/code/environments/production/manifests

Create a site.pp file, and include the Apache module for each agent node. Also input the variables for the adminemail and servername parameters. Your site.pp should resemble the following:


12345678910111213141516node 'puppet-agent-ubuntu.example.com' {  $adminemail = 'webmaster@example.com'  $servername = 'puppet.example.com'   include apache  include apache::vhosts  } node 'puppet-agent-centos.example.com' {  $adminemail = 'webmaster@example.com'  $servername = 'puppet.example.com'   include apache  include apache::vhosts   }

By default, the Puppet agent service on your managed nodes will automatically check with the master once every 30 minutes and apply any new configurations from the master. You can also manually invoke the Puppet agent process in-between automatic agent runs. To manually run the new module on your agent nodes, log in to the nodes and run:

sudo /opt/puppetlabs/bin/puppet agent -t

Now that we have learned how to create a module from scratch, let’s learn how to use a pre-existing module from the puppet forge of puppetlabs.

Use a module from PuppetForge 

Puppet Forge already has many modules for the server to run. We can configure these just as extensively as a module you created and can save time since we need not create the module from scratch.

Ensure you are in the /etc/puppetlabs/code/environments/production/modules directory and install the Puppet Forge’s MySQL module by PuppetLabs. This will also install any prerequisite modules.

cd /etc/puppetlabs/code/environments/production/modules

1sudo /opt/puppetlabs/bin/puppet module install puppetlabs-mysql

Use Hiera to Create Databases

Before you create the configuration files for the MySQL module, consider that you may not want to use the same values across all agent nodes. To supply Puppet with the correct data per node, we use Hiera. You will use a different root password per node, thus creating different MySQL databases.

  • Navigate to /etc/puppet and create Hiera’s configuration file hiera.yaml in the main puppet directory. You will use Hiera’s default values:


12345678---version: 5hierarchy:  - name: Common    path: common.yamldefaults:  data_hash: yaml_data  datadir: data
  • Create the file common.yaml. It will define the default root password for MySQL:


1mysql::server::root_password: 'password'

We use the common.yaml file when a variable is not defined elsewhere. This means all servers will share the same MySQL root password. These passwords can also be hashed to increase security.

  • To use the MySQL module’s defaults you can add an include ‘::mysql::server’ line to the site.pp file. However, in this example, you will override some of the module’s defaults to create a database for each of your nodes.

Edit the site.pp file with the following values:

123456789101112131415161718192021222324252627282930node 'Puppetagent-ubuntu.example.com' {  $adminemail = 'webmaster@example.com'  $servername = 'hostname.example.com'  include apache  include apache::vhosts  include mysql::server  mysql::db { "mydb_${fqdn}":    user     => 'myuser',    password => 'mypass',    dbname   => 'mydb',    host     => $::fqdn,    grant    => ['SELECT', 'UPDATE'],    tag      => $domain,  }}node 'Puppetagent-centos.example.com' {  $adminemail = 'webmaster@example.com'  $servername = 'hostname.example.com'  include apache  include apache::vhosts  include mysql::server  mysql::db { "mydb_${fqdn}":    user     => 'myuser',    password => 'mypass',    dbname   => 'mydb',    host     => $::fqdn,    grant    => ['SELECT', 'UPDATE'],    tag      => $domain,  } }

Automating the installation of the Puppet Modules from puppet master to puppet agent

  • You can run these updates manually on each node by SSHing into each node and issuing the following command:

sudo /opt/puppetlabs/bin/puppet agent -t

  • Otherwise, the Puppet agent service on your managed nodes will automatically check with the master once every 30 minutes and apply any new configurations from the master. 

Click here to read more blogs of us

Click here to read Q&A

About Author

After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.

1 thought on “Puppet Modules Use for IT Infrastructure Automation”

  1. Since I am new to IT, I didn’t know what type of questions would I get in the interview. These Interview questions and answers article help me to get clear understanding on both questions and answers.

Leave a Comment