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

top 
scope
node 
scope
child 
class
class
facter

params

user defined

params

top 
scope
node 
scope
child 
class
parent 
class

user defined 

scope

  • Specific area in puppet's code hirearchy
  • allows to define same param from different places
  • to create global/local params

top scope

node scope

  • global params
  • apply to all nodes
  • node specific
    

group lab


$color = 'blue'
$car   = 'maruti' 

node 'node1' {

  include tomcat
  
}


node 'node2' {

   $color = 'green'
   include tomcat

}

app.pp

where to use ?

  • resource names
  • parameter value 
  • template variable

 

param reference

$color
${color}
${color}

"

"

group lab

class tomcat::scope {

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

}

class tomcat::scope

group lab

class tomcat {

  include tomcat::scope
  include tomcat::install
  include tomcat::config
  include tomcat::service 
}

class tomcat

discussion

Run puppet on node1 and node2 both

  • Do you see same values printed on both nodes? 
  • If you see different values, why? 

top scope

node scope

parent class

child class

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
  • created as a design pattern to define the sane defaults

Sane Defaults

group lab

class tomcat::params {

  $color = 'white' 
  $car   = 'figo'

}

tomcat::params

  • apply and check if the nodes pick up the properties in params.pp
  • how would you make properties from one class available into another

inheritance

group lab

class tomcat::scope inherits tomcat::params{

   $color = 'yellow'


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

}

tomcat::scope

qualified params

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

group exercise

Parameterizing Tomcat Configs

group exercise

tomcat::params

class tomcat::params {

  $color = 'white' 
  $car   = 'figo'
  $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 
}

refactor classes

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

And to use these params, we 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

By School of Devops

PUP : Parameters

PUP 05 - Puppet Parameters, Facts and Templates

  • 1,567