parameters

managing properties

TOPICS

  • Code vs data

  • Parameters

  • Scoping

  • Inheritance

  • Facts

Code          DATA

vs

  • one module

dev

stage

prod

  • different 
    envionments
  • different 
    platforms

centos

ubuntu

bsd

Modules without data

  • Creates generic modules which can be

shared

reused

  • save tonnes of time

Code             |               DATA

manifests

templates

system defined

user defined

params

hiera
params.pp class
facter

params

user defined

params

hiera
params.pp

class

user defined 

story of defaults

  • Modules define the default properties
  • Defaults are overridden from outside the modules 
  • Overriding of properties happens either from node definitions, or external data sources
  • This makes modules generic and sharable

params.pp

hiera

  • old way
  • new way, with puppet 4

defaults inside modules

  • design pattern, mixed with existing classes
  • well though after solution, can scale, simplified data management
  • dedicated place to store data
  • can get complex with increasing properties and platform specific logic

params.pp

the story of sane defaults

params.pp

  • dedicated class to define data/properties/params
  • can be also used to write logic/conditionals to separate system specific properties

Sane Defaults

group lab

class prefs::params {

  $color = 'white' 
  $car   = 'ford'

}

prefs::params

  • lets create a params class for prefs module
  • define two params $color and $car
puppet module generate prefs --skip-interview user-prefs

defining params with params.pp

group lab

group lab

where to use params

  • resource names
  • parameter value 
  • template variable

 

file { $::tomcat::config_path:
    owner    => $::tomcat::user, 
    group    => $::tomcat::group, 
    mode     => '0644',
  }
<%= @max_connections %> 
class prefs::config {

   notify{"print the prefs": 
     message => "
     
       FAVOURITE COLOR :  ${color}
       FAVOURITE CAR   :  ${car}
    
    "
  }

}

class prefs::config

group lab

  • lets write a class to consume the params defined earlier

group lab

class prefs {

  include prefs::config

}

class prefs

node node1 {

  include tomcat
  include prefs

}

app.pp

  • to apply this class on a node, it needs to be called from the init.pp of prefs module
  • prefs class has to be called from the node definition in order for it to be applied

inheritance

grandfather

father

son

params.pp

init.pp

config.pp

inherits xyz::params

inherits xyz

group lab

class prefs::config inherits prefs{



   notify{"print the scope": 
     message => "
     
       FAVOURITE COLOR :  ${color}
       FAVOURITE CAR   :  ${car}
    
    "
  }

}

prefs::config

class prefs inherits prefs::params{

  include prefs::config

}

class prefs

prefs::params

qualified params

$::tomcat::params::color
$::tomcat::color
$color

lab exercise

Parameterizing Tomcat Configs

group exercise

tomcat::params

class tomcat::params {

 
  $user  = 'tomcat'
  $group = 'tomcat'
  $config_path  = '/etc/tomcat/tomcat.conf'
  $packages  = [ 'tomcat', 'tomcat-webapps' ]
  $service_name = 'tomcat'
  $service_state = running

}

define params

group exercise

file: cookbooks/tomcat/attributes/default.rb

default['tomcat']['user'] = 'tomcat'
default['tomcat']['group'] = 'tomcat'
default['tomcat']['config'] = '/etc/tomcat/tomcat.conf'
default['tomcat']['user_config'] = '/etc/tomcat/tomcat-users.xml'
default['tomcat']['packages'] = [ 'tomcat', 'tomcat-webapps' ]
default['tomcat']['service'] = 'tomcat'

define attributes

cd myapp
chef generate attribute cookbooks/tomcat default
class tomcat
class tomcat inherits tomcat::params{

  include tomcat::scope
  include tomcat::install
  include tomcat::config
  include tomcat::service 
}
class tomcat::install inherits tomcat{
    
    include java
    
    package { $::tomcat::packages:
      ensure   => installed, 
      require  => Package['epel-release']
    }

}
tomcat::install
class tomcat::config inherits tomcat{

  file { $::tomcat::config_path:
    source    => 'puppet:///modules/tomcat/tomcat.conf',
    owner    => $::tomcat::user, 
    group    => $::tomcat::group, 
    mode     => '0644',
    notify   => Service['tomcat'] 
  }

}
tomcat::config
class tomcat::service inherits tomcat{

   service { $::tomcat::service_name:
     ensure   => $::tomcat::service_state, 
     enable   => true,
     require  => Class['tomcat::install'],
   }
}
tomcat::service

apply

group exercise

handle platform specific configs

lets convert base.pp recipe into a module which can be applied to all linux nodes to perform common tasks.....
cd /workspace/code/environments/production/modules

puppet module generate --skip-interview user-base

mv /workspace/base.pp base/manifests/init.pp

Generate base module

group exercise

class base{

    user {"deploy" :
      ensure    => present, 
      uid       => 5001, 
      password  => '$1$WD98.uaZ$cxx30x/K3FXQrljxsvBIu/',
      home      => '/home/deploy'
    }
    
    user {"dojo" :
      ensure  => absent, 
    }
    
    package { [ "tree", "unzip", "git", "ntp", "wget"  ]:
    
      ensure  => installed
    
    }
    
    service { "ntp": 
      ensure  => running, 
      enable  => true, 
      
    }
    
}

group exercise

class base


node 'node2' {

   include base

   $color = 'green'
   include tomcat

}

group exercise

Add the base class to node definition for node2

Question : What happens when you apply this class on node2 by running puppet agent ??

discussion

knife node run_list add app1 "recipe[base]"
sudo chef-client

Add default recipe from base to run list

Apply

note: change this

Failes because service "ntp" is not available on centos.  It was created for ubuntu. 

NTP 

ubuntu

centos

package

service

ntp
ntp
ntp
ntpd

discussion

group exercise

class base::params {

  case $::os['family'] {
    'Debian': {
      $ntp_service = 'ntp'
    }
    'RedHat': {
      $ntp_service = 'ntpd'
    }
  }
  

}
create base::params class

file:   modules/base/manifests/params.pp

Exercise

Refactor the code to use the parameter just defined in base::params.  You will need to, 

  • inherit base::params in base class (init.pp)
  • parameterize the service name

exercise

facter

  • System Profiler
  • Installed with puppet
  • Provides facts
system defined

attributes

  • Platform
  • Network
  • Kernel
  • Memory
  • CPU
  • Virtualization
  • Disk Mounts
  • Other system info
agent
master
facts
catalog
report

facter

facter 
facter ipaddress
facter hostname
facter memory
facter memory.system 
facter memory.system.total
facter processors
facter processors.count
system defined

attributes

 Finding facts
 about a  Node with facter

referencing

facts

attributes

system defined
$facts['hostname']
${::hostname}
${hostname}
$hostname

referencing

facts

${::ipaddress}

attributes

system defined
${::hostname}
${::memory}
${::memory['system']}
${::processors['count']}
facter 
facter ipaddress
facter hostname
facter memory
facter memory.system 
facter memory.system.total
facter processors
facter processors.count

group exercise

    file { '/etc/motd': 
      ensure   => file, 
      owner    => 'root',
      content  => "
      
         This server is a property of XYZ Inc.
         
         SYSTEM INFO 
         ============
         
         Hostname     : ${::fqdn}
         IP Address   : ${::ipaddress}
         Memory       : ${::memory['system']['total']}
         Cores        : ${::processors['count']}
         OS           : ${::os['distro']['description']}
      
      
      "
    
    }
Update base class

file:   modules/base/manifests/init.pp

PUP : Parameters (puppet 4)

By School of Devops

PUP : Parameters (puppet 4)

PUP 05 - Puppet Parameters, Facts and Templates

  • 988