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

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

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

# Including the class 
class { 'test': }
# Including the class - with parameter
class { 'test': 
  param => 'sandesh',
} 
# using Include
include test
include ::test   # scope?

# same result
class {'test':}
include test
include ::test

Clone this wiki locally