Home / Puppet / Puppet – Writing a First Manifest – Modules

Puppet – Writing a First Manifest – Modules

It’s time to write first manifest on the puppet server. This article is going to brief about writing a custom  puppet script  to automate the installation of httpd/Apache package on Linux servers. Similar way you can automate package installation, updating files, configuring services and many more. Resources are defined using resource  declaration syntax  and stored on file with .pp extension. This file is called manifest.  These manifest must be stored in codedir  to auto load on puppet.

 

  • Puppet Server : uaha  (RHEL7)
  • Puppet Agent Node: uapa1   (RHEL7)

 

Let’s write a first manifest.

1.Login to puppet master server as root and check the existing environment.

[root@UA-HA ~]# puppet --version
4.3.1
[root@UA-HA ~]# cd /etc/puppetlabs/code/environments/
[root@UA-HA environments]# ls -lrt
total 0
drwxr-xr-x 4 pe-puppet pe-puppet 70 Feb 8 14:18 production
[root@UA-HA environments]#

 

2. Navigate to “/etc/puppetlabs/code/environments/production/manifests” directory and edit the site.pp like below.

node default {
 # This is where you can declare classes for all nodes.
package { httpd: ensure => installed; }
}

Here, I have just added the line “package { httpd: ensure => installed; }” in site.pp file. This will ensure that httpd packages will be installed on all the nodes which are configured under production environment.

 

In my case, Puppet agents are Redhat enterprise Linux and that’s why I have used “httpd” as package name. If you have Debian variant of puppet agents , you must use “apache2”.Using modules , we can solve this kind of discrimination 

 

3. Login puppet agent node and apply the configuration from puppet master immediately.

[root@uapa1 ~]# puppet agent -t

Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for uapa1
Info: Applying configuration version '1455158556'
Error: Execution of '/usr/bin/yum -d 0 -e 0 -y install httpd' returned 1: Error downloading packages:
httpd-2.4.6-40.el7.x86_64: [Errno 256] No more mirrors to try.
Error: /Stage[main]/Main/Node[default]/Package[httpd]/ensure: change from purged to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y install httpd' returned 1: Error downloading packages:
httpd-2.4.6-40.el7.x86_64: [Errno 256] No more mirrors to try.
Notice: Applied catalog in 5.20 seconds
[root@uapa1 ~]#

You can see that puppet agent is trying to install “httpd” package but it got failed due to yum repository issue. If you have already configured the valid yum repository , httpd installation should be succeeded.

 

Once you have configured the yum repository , you could see that package installation got succeeded.

[root@uapa1 ~]# puppet agent -t
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for uapa1
Info: Applying configuration version '1455160622'
Notice: /Stage[main]/Main/Node[default]/Package[httpd]/ensure: created
Notice: Applied catalog in 6.55 seconds
[root@uapa1 ~]# rpm -qa httpd
httpd-2.4.6-40.el7.x86_64
[root@uapa1 ~]#

You no need to login to puppet agent node to pull the configuration from master. It automatically checks with puppet master server for every 30 minutes and applies the configuration.

Adding codes in manifests directory and site.pp is not sufficient in larger environment.If you use, then it will results to increase the duplicate and complex coding’s. To eliminate this problem , Puppet has concept called modules.

 

Delete the following line from site.pp file to  demonstrate the module’s part.

package { httpd: ensure => installed; }

 

 

What is a module in Puppet?

 

Module is nothing but a collection of manifests,files,templates,classes etc. We can call a module as  portable manifest.  Module should follow the specific rules to load in puppet automatically. Each and every module in puppet must be constructed by keeping it’s structure in mind. A module must contain all the required files and directories to function properly.

Let’s create a new module on “production” environment.

1.Login to the puppet server and navigate to directory “/etc/puppetlabs/code/environments/production” .

[root@UA-HA production]# cd /etc/puppetlabs/code/environments/production
[root@UA-HA production]# ls -lrt
total 4
-rw-r--r-- 1 pe-puppet pe-puppet 879 Jan 27 10:38 environment.conf
drwxr-xr-x 2 pe-puppet pe-puppet  49 Feb 10 21:51 manifests
drwxr-xr-x 5 root      root       50 Feb 10 23:26 modules
[root@UA-HA production]#

 

2. Create new module called “httpd” and it’s subdirectories.

[root@UA-HA production]# mkdir -p modules/httpd/{files,templates,manifests,examples}

 

3. It’s good to have “tree” package on system to view the directory structure like following. Navigate to modules directory and check the tree view for httpd module.

[root@UA-HA modules]# tree httpd/
httpd/
├── examples
├── files
├── manifests
└── templates

4 directories, 0 file
[root@UA-HA modules]#
New module name is “httpd” and we might call this name during the manifest creation. (Directory name)

 

4. Navigate to manifests directory and create file called init.pp like following.

[root@UA-HA manifests]# cat init.pp
class httpd {
      package { httpd:
        ensure => present,
   }
}
[root@UA-HA manifests]#

We have successfully created manifest to install package “httpd”.

 

5. Navigate back to “/etc/puppetlabs/code/environments/production/manifests” and create “node.pp” . This file should contain list of nodes where the “httpd” package needs to be installed.

[root@UA-HA manifests]# cat nodes.pp
node uapa1 {
  include httpd
}
[root@UA-HA manifests]#

Here , we are just calling “httpd” module.

 

My Puppet agent node has registered without FQDN and that’s the reason I have used hostname instead of FQDN.

[root@UA-HA manifests]# puppet cert list --all |grep uapa1
+ "uapa1"                                         (SHA256) 0B:DF:54:97:91:E6:9A:15:71:B8:FF:53:CF:C3:09:C4:3A:0E:EB:66:00:EB:14:3B:49:9A:4B:03:D2:45:48:D4
[root@UA-HA manifests]#

 

6. Login to puppet agent node and execute the following command to pull the configuration.

[root@uapa1 ~]# puppet agent -t
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for uapa1
Info: Applying configuration version '1455167528'
Notice: /Stage[main]/Httpd/Package[httpd]/ensure: created
Notice: Applied catalog in 4.50 seconds
[root@uapa1 ~]# rpm -qa httpd
httpd-2.4.6-40.el7.x86_64
[root@uapa1 ~]#

Here you can see that httpd package has been successfully installed on puppet agent node. You can add N-number of puppet agent in node.pp to trigger the installation simultaneously on list of servers.

 

In up coming articles, we will see some more examples about different resource configuration using modules.

You no need to worry about writing the custom modules . You could find ready made modules in puppet-forge for most of the activities. We will see about puppet-forge later.

 

Hope this article is informative to you . Share it ! Comment it !! Be Sociable !!!

VMTURBO-CLOUD-CAPACITY

One comment

  1. Awesome , great work keep it up.
    Normally i daily check your site for new stuff.Thanks for hardwork and commitment

Leave a Reply

Your email address will not be published. Required fields are marked *