Code vs data
Parameters
Scoping
Inheritance
Facts
vs
one module
dev
stage
prod
different
envionments
different
platforms
centos
ubuntu
bsd
Creates generic modules which can be
shared
reused
save tonnes of time
top scope
node scope
child class
class
facter
top scope
node scope
child class
parent class
top scope
node scope
global params
apply to all nodes
node specific
$color = 'blue'
$car = 'maruti'
node 'node1' {
include tomcat
}
node 'node2' {
$color = 'green'
include tomcat
}
app.pp
resource names
parameter value
template variable
$color
${color}
${color}
"
"
class tomcat::scope {
notify{"print the scope":
message => "
FAVOURITE COLOR : ${color}
FAVOURITE CAR : ${car}
"
}
}
class tomcat::scope
class tomcat {
include tomcat::scope
include tomcat::install
include tomcat::config
include tomcat::service
}
class tomcat
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
class tomcat::params {
$color = 'white'
$car = 'figo'
}
tomcat::params
class tomcat::scope inherits tomcat::params{
$color = 'yellow'
notify{"print the scope":
message => "
FAVOURITE COLOR : ${color}
FAVOURITE CAR : ${car}
"
}
}
tomcat::scope
$::tomcat::params::color
$::tomcat::color
$color
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
}
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'
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
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
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,
}
}
class base
node 'node2' {
include base
$color = 'green'
include tomcat
}
Add the base class to node definition for node2
Question : What happens when you apply this class on node2 by running puppet agent ??
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
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
System Profiler
Installed with puppet
Provides facts
system defined
agent
master
facts
catalog
report
facter
facter ipaddress
facter hostname
facter memory
facter memory.system
facter memory.system.total
facter processors
facter processors.count
system defined
Finding facts about a Node with facter
system defined
$facts['hostname']
${::hostname}
${hostname}
$hostname
${::ipaddress}
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
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