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
hiera
params.pp class
facter
hiera
params.pp
class
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
old way
new way, with puppet 4
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
class prefs::params {
$color = 'white'
$car = 'ford'
}
prefs::params
puppet module generate prefs --skip-interview user-prefs
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
class prefs {
include prefs::config
}
class prefs
node node1 {
include tomcat
include prefs
}
app.pp
inherits xyz::params
inherits xyz
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
$::tomcat::params::color
$::tomcat::color
$color
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
}
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
Refactor the code to use the parameter just defined in base::params. You will 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