Skip to content

Puppet 4 :: Modules and Classes

Sandesh Kota edited this page Jun 26, 2018 · 17 revisions

Environments -> Modules -> classes -> parameters -> Inheritance -> Custom Resources

Environments & Modules

  • Env: Support for Production and Development Code
  • Env: puppet config print environment
  • Env: Support for Isolation of Departmental Code
  • Env: puppet config print environmentpath
  • Env: Helps in using different versions of the module for each environment
  • Module: Can be located in either the environment directory or a common shared directory
  • Create environment in /etc/puppetlabs/code/environments/dev/{modules,manifests}
  • Install module as follows puppet module install ntp --environment dev
  • Module: To see the list puppet module list --environment dev
  • Puppet configuration /etc/puppetlabs/puppet/puppet.conf
  • To work under a environment puppet config set environment dev

Modules

  • Puppet module is simply a directory tree with specific and predictable content
  • Module contains
    • Name (ntp/)
      • manifests/init.pp
      • files/
      • lib/ # can contain facts
      • facts.d/ # external facts
      • templates/ # erb (embeded ruby file) | Puppet4 -> epb (embeded manifest file)
      • examples/
  • manifest must contain a class with the same name as the module
  • Modules Metadata
    • we can use puppet module generate skota/test # test = module name, skota = user name/ org name
      • this will start an interview process to collect other information i.e, version no, author etc..
    • To skip all we can use puppet module generate skota/test --skip-interview - This will use the defaults
    • stores data in metadata.json
    • This will help us in versioning our Modules
  • tree module will list down the file structure

Sub Classes

ntp/manifests/config.pp    # create a file name under module's manifest folder

class ntp::config {        # class name should be prefixed by base class as shown (ntp::)
}

include ntp::config                # usage 1
notify => Class['ntp::config']     # usage 2

Parameterized Classes

  • Examples below:
class test ($param = 'daily') {            # $param is a parameter and it's default value is 'daily'
  file {'temp':
    ensure => 'present',
    content => file("etc/${param}"),      # run-time error if "etc/daily" file doesn't exists
  }
}

class test (String $param = 'weekly') {     # Ensuring that only string type can be sent as $param 

# Including the class 
class { 'test': }

# Including the class - with parameter
class { 'test': 
  param => 'weekly',
} 

# using Include
include test
include ::test   # scope?

# same result
class {'test':}
include test
include ::test
  • Validation
    • we can use validation functions that are provided by puppetlabs/stdlib
      • Ex: validate_re(<string_to_test>, <regular_expression>)
    • Implementation example
    class test ($param = 'daily') {
      $allowed = [
                   '^daily$',    # starts with daily ^
                   '^weekly$'      # ends with weekly $
                 ]
    
      validate_re($param, $allowed)    # will print out a message saying the allowed param (daily, weekly)
    
      file {'temp':
        ensure => 'present',
        content => file("etc/${param}"),      # run-time error if "etc/daily" file doesn't exists
      }
    }
    

Clone this wiki locally