Site icon UnixArena

Puppet – How to Classify the Node types ?

Puppet Classying OS variant

Puppet Classying OS variant

We need to classify the nodes  when you have to perform similar operation on different OS variant. For an example, If you want to install Apache package on Linux systems, you need to use package name as “httpd” on Redhat variants  and “apache2” on Debian variants.  In such a cases, you need to tweak your module to detect the OS family using facter and based upon the results , module have to choose either “httpd” or “apache2”. Let’s write a small code to achieve this.

 

1. Login to the puppet server as root user.

2.Navigate to “production” environment’s module directory.

[root@UA-HA ~]# cd /etc/puppetlabs/code/environments/production/modules/
[root@UA-HA modules]# ls -lrt
total 0
drwxr-xr-x 3 root root 22 Feb  8 14:16 helloworld
drwxr-xr-x 6 root root 65 Feb  8 15:15 accounts
drwxr-xr-x 6 root root 65 Feb 10 23:36 httpd
drwxr-xr-x 5 root root 50 Feb 14 07:18 ntpconfig
drwxr-xr-x 5 root root 50 Feb 14 09:02 filetest
drwxr-xr-x 5 root root 50 Feb 14 10:55 testdirs
drwxr-xr-x 5 root root 50 Feb 15 14:11 sshdroot
[root@UA-HA modules]#

 

3. Create the new module structure for Apache installation.

[root@UA-HA modules]# mkdir -p  apachehttpd/{files,manifests,templates}
[root@UA-HA modules]#
[root@UA-HA modules]# tree apachehttpd
apachehttpd
├── files
├── manifests
│   └── init.pp
└── templates

3 directories, 1 file
[root@UA-HA modules]#

 

4.Navigate to manifest directory and create file called init.pp.

[root@UA-HA modules]# cd apachehttpd/manifests/
[root@UA-HA manifests]# vi init.pp
[root@UA-HA manifests]# cat init.pp
class apachehttpd {

$package_name = $osfamily ? {
   'RedHat' => 'httpd',
   'Debian' => 'apache2',
   default  =>  undef,
 }
    # Install Apache package
    package { "$package_name":
        ensure => installed,
        alias  => "apache",
    }

    # Enable Apache service
    service { "$package_name":
        ensure => running,
        enable => true,
        require => Package['apache']
    }

}
[root@UA-HA manifests]#

 

5. Navigate back to the main manifest directory and call the “apachehttpd” module for node uapa1.

[root@UA-HA manifests]# cd ../../../manifests/
[root@UA-HA manifests]# ls -lrt
total 8
-rw-r--r-- 1 pe-puppet pe-puppet 1226 Feb 10 23:44 site.pp
-rw-r--r-- 1 root      root        34 Feb 16 01:01 nodes.pp
[root@UA-HA manifests]#
[root@UA-HA manifests]# cat nodes.pp
node uapa1 {
  include apachehttpd
}
[root@UA-HA manifests]#

 

6. Login to puppet agent nodes and run the puppet agent test to see the results 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 '1455607480'
Notice: /Stage[main]/Apachehttpd/Package[httpd]/ensure: created
Notice: /Stage[main]/Apachehttpd/Service[httpd]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Apachehttpd/Service[httpd]: Unscheduling refresh on Service[httpd]
Notice: Applied catalog in 4.02 seconds
[root@uapa1 ~]#

 

7.Verify our work.

[root@uapa1 ~]# rpm -qa httpd
httpd-2.4.6-40.el7.x86_64
[root@uapa1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2016-02-19 18:09:42 EST; 1min 13s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 48461 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─48461 /usr/sbin/httpd -DFOREGROUND
           ├─48462 /usr/sbin/httpd -DFOREGROUND
           ├─48463 /usr/sbin/httpd -DFOREGROUND
           ├─48464 /usr/sbin/httpd -DFOREGROUND
           ├─48465 /usr/sbin/httpd -DFOREGROUND
           └─48469 /usr/sbin/httpd -DFOREGROUND

Feb 19 18:09:42 uapa1 systemd[1]: Starting The Apache HTTP Server...
Feb 19 18:09:42 uapa1 httpd[48461]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.203.134. Set th...is message
Feb 19 18:09:42 uapa1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@uapa1 ~]#

 

This is very small demonstration to perform the similar task on different OS variant. Node classification is achieved using “facter” mechanism.

 

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

Exit mobile version