Basic Puppet

Earlier today I was helping a friend get started learning puppet.  My favorite configuration management system.  I figured some of the basics I shared might help others.  His focus was on how to handle some very basic things in multiple operating systems.  In these examples we will assume that I am working with two flavors of Linux RHEL and Suse.

 

Set ownership and file permissions:

file { "/etc/passwd":
        owner => $operatingsystem ? {
                redhat => "root",
                suse => "root",
                default => "root",
        },
        group => $operatingsystem ? {
                redhat => "root",
                suse => "wheel",
                default => "root",
        },
        mode => $operatingsystem ? {
                redhat => "644",
                suse => "664",
                default => "644",
        },
} # end of file passwd

 

Simple process with an operating system switch.  The variables you can trigger off in puppet can be found with the facter command.   Once you understand your variables using case operators can be really powerful.

 

Push out a file with different sources:

file { "ntpdconfig":
                name => "/etc/ntp.conf",
                ensure => present,
                replace => true,
                owner => 'root',
                group => 'root',
                mode => '0644',
                source => $operatingsystem ? {
                        redhat => "puppet:///files/redhat/ntp.conf",
                        suse => "puppet:///files/suse/ntp.conf",
                        default => "puppet:///files/redhat/ntp.conf",
                };
        }

Push out the file with different local sources by OS and replace the file if already present.

 

One of the things I love about puppet is the ability to to service checks.  For example ensure that httpd is running and set to start at boot time.  Sometimes the ability to check for the running service requires a custom setting which you can do with the following:

service { tivoli:
                name => "startscript",
                enable => true,
                status => "/bin/ps -ef | /bin/grep startscript_process_name",
                ensure => running,
}

In this case we can use the ps command to check for the process as running.

 

From time to time you need to push out a file and execute if something changes:

 

file { “/scripts/bob.sh”:

ensure => present,

replace => true,

owner => ‘root’,

group => ‘root’,

mode => ‘0750’,

source => “puppet:///files/common/bob.sh”,

}

exec {“bob”:

cwd => “/scripts”,

command => “/scripts/bob.sh”,

require => file[“/scripts/bob.sh”],

timeout => 0,

subscribe => [ File[“/scripts/bob.sh”] ],

}

 

Simple… push out the file bob.sh and if it changes execute it.

 

Here is a template for running code for different operating systems:

case $lsbdistid {
        #-----------RHEL---------------------
        RedHatEnterpriseServer: {

                case $lsbmajdistrelease {
                        5: {
                                                   } #End of RHEL 5
                        6: {
                                                   } #End of RHEL 6
                                                7: {
                                                   } #End of RHEL 7

                } # End of RHEL
                #-----------SUSE Enterprise not for VMware
                Suse: {
                                                11: {
                                                    } #End of Suse 11
                } #End of Suse Enterprise
} #End of lsbdistid

 

Hope it helps.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.