CHAPTER 4
modules are the packages with manifests and supporting files
have 1:1 mapping with the applications
let you create a library of reusable code
manifests
files
templates
data
spec
examples
metadata.json
README.MD
master-code-dir: /etc/puppetlabs/code
master-code-dir: /workspace/code
cp -r /etc/puppetlabs/code /workspace/code
master-code-dir: /etc/puppetlabs/code
master-code-dir: /workspace/code
service puppetserver restart
Edit File : /etc/puppetlabs/puppetserver/conf.d/puppetserver.conf
Copy existing structure to our workspace
Change from
Change to
Restart Puppet Master
You have been asked to deploy a java application server with tomcat. You have been tasked to create automation code with puppet to set it up.
Problem Statement
Solution:
Generate modules for java and tomcat
Create class to install java and
Create a node definition to apply the classes
Create classes to install tomcat and start the service, apply
Write classes to manage configuration files
puppet module is a utility which comes with code generator
it can let you create, search install, upload modules (to and from puppet forge)
root@puppet:/workspace# puppet help module
USAGE: puppet module <action> [--environment production ] [--modulepath ]
ACTIONS:
build Build a module release package.
changes Show modified files of an installed module.
generate Generate boilerplate for a new module.
install Install a module from the Puppet Forge or a release archive.
list List installed modules
search Search the Puppet Forge for a module.
uninstall Uninstall a puppet module.
upgrade Upgrade a puppet module.
cd /workspace/code/environments/production/modules
puppet module generate --skip-interview user-java
puppet module generate --skip-interview user-tomcat
|-- java
| |-- Gemfile
| |-- README.md
| |-- Rakefile
| |-- examples
| | `-- init.pp
| |-- manifests
| | `-- init.pp
| |-- metadata.json
| `-- spec
| |-- classes
| | `-- init_spec.rb
| `-- spec_helper.rb
-- java
| |-- Gemfile
| |-- README.md
| |-- Rakefile
| |-- examples
| | `-- init.pp
| |-- manifests
| | `-- init.pp
| |-- metadata.json
| `-- spec
| |-- classes
| | `-- init_spec.rb
| `-- spec_helper.rb
modules
tomcat
manifests
init.pp
????
????
modules
tomcat
manifests
init.pp
abc.pp
xyz.pp
class tomcat::abc
class tomcat::xyz
subdir
pqr.pp
class tomcat::subdir::pqr
modules
tomcat
manifests
init.pp
install.pp
config.pp
service.pp
deploy.pp
ssl.pp
phases
tomcat::service
tomcat::config
tomcat::install
classes
phases
java::install
classes
class java::install {
package { [ 'epel-release', 'java-1.7.0-openjdk'] :
ensure => installed,
}
}
file: modules/java/manifests/install.pp
Generate modules for java and tomcat
Create class to install java and
Create a node definition to apply the classes
Create classes to install tomcat and start the service, apply
Write classes to manage configuration files
now that we have written the class, lets learn how to apply it, in a client server model
node
list of classes
node 'nodename' {
include class1
include class2
include class3, class4
class { 'class3':
param1 => val1,
param2 => val2,
param3 => val3,
}
}
node 'app1.example.io', 'app2.example.io', 'app3.example.io' {
include class1
include class2
}
node /^(app|web)\.blr\d+\.example\.io$/ {
include class1
include class2
}
groups
regex
simple
node definition provides a mapping between nodes and a list of classes to apply
node definition resides on the puppet master
its the simplest way of classifying nodes
there are more options available for node classification
ENC
Puppet Entperprise
code
environments
xxx
manifests
xxx.pp
webservers.pp
db.pp
modules
code
environments
production
manifests
app.pp
node 'node1' {
include java::install
}
file: environments/production/manifests/app.pp
ssh devops@node1
sudo su
puppet agent -t
ssh devops@node2
sudo su
puppet agent -t
app.codespace.io
app.codespace
app
default
default block
node default {
notify{'checkpoint_1':
message => '
CHECKPOINT_1
DEFAULT BLOCK APPLIED
Looks like there is no node definition for this host
'
}
}
file: production/manifests/site.pp
notify{'checkpoint_1':
message => '
CHECKPOINT_1
DEFAULT BLOCK APPLIED
Looks like there is no node definition for this host
'
}
notify
notify{'checkpoint_1':}
package { 'httpd':
ensure => absent,
}
package {'nginx':
ensure => $nginx_version,
require => Package['httpd'],
}
What would this code do?
why ?
how ?
A
B
->
package
service
before B
require A
C
D
~>
file
service
notify D
subscribe C
package { 'nginx':
ensure => installed,
before => Service["nginx"],
}
file { 'nginx.conf':
ensure => file,
mode => '0644',
notify => Service["nginx"],
}
service { 'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
require => [ Package["nginx"], File["nginx.conf"] ]
subscribe => File["nginx.conf"],
}
Package["nginx"] -> File["nginx.conf"] ~> Service["nginx"]
sample ordering.pp
Type['title']
CAPS
before
require
notify
subscribe
Meta Parameters
---------------
alias, audit, before, consume, export,
loglevel, noop, notify, require,
schedule, stage, subscribe, tag
puppet describe -sm package
Generate modules for java and tomcat
Create class to install java and
Create a node definition to apply the classes
Create classes to install tomcat and start the service, apply
Write classes to manage configuration files
create a tomcat::service recipe to start and enable tomcat service
Service should depends on package tomcat
create a tomcat::install recipe to install tomcat along with example apps. Packages to install are
Create and apply the following recipes for tomcat
http://IPADDR:8081
class tomcat {
include java::install
include tomcat::install
include tomcat::service
}
Lets call all other manifests from init.pp
node 'node1' {
include tomcat
}
node 'node2' {
include tomcat
}
{
"name": "user-tomcat",
"version": "0.1.0",
"author": "user",
"summary": null,
"license": "Apache-2.0",
"source": "",
"project_page": null,
"issues_url": null,
"dependencies": [
{"name":"puppetlabs-stdlib","version_requirement":">= 1.0.0"}
],
"data_provider": null
}
Generate modules for java and tomcat
Create class to install java and
Create a node definition to apply the classes
Create classes to install tomcat and start the service, apply
Write classes to manage configuration files
We will need to manage configurations eg. tomcat.conf
since chef is a centralized configuration management system, we will keep the files centrally in cookbooks, which will then be copied to all managed nodes
Create tomcat.conf file in tomcat modules' files directory.
add tomcat::config class to copy these files to the relevant locations on destination hosts
modules
tomcat
manifests
init.pp
install.pp
service.pp
config.pp
files
tomcat.conf
destination path in nodes: /etc/tomcat/tomcat.conf
Generate tomcat.conf using chef generate file in tomcat cookbook directory.
add tomcat::config recipe to copy these files to the relevant locations on destination hosts
cookbooks
tomcat
recipes
default.rb
install.rb
service.rb
config.rb
files
tomcat.conf
tomcat-users.xml
path: /etc/tomcat/tomcat-users.xml
path: /etc/tomcat/tomcat.conf
file: cookbooks/tomcat/files/default/tomcat.conf
TOMCAT_CFG_LOADED="1"
JAVA_HOME="/usr/lib/jvm/jre"
JAVA_OPTS="-Xms64m -Xmx128m -XX:MaxPermSize=128M -Djava.security.egd=file:/dev/./urandom"
CATALINA_BASE="/usr/share/tomcat"
CATALINA_HOME="/usr/share/tomcat"
JASPER_HOME="/usr/share/tomcat"
CATALINA_TMPDIR="/var/cache/tomcat/temp"
TOMCAT_USER="tomcat"
SECURITY_MANAGER="false"
SHUTDOWN_WAIT="30"
SHUTDOWN_VERBOSE="false"
CATALINA_PID="/var/run/tomcat.pid"
class tomcat::config {
file { '/etc/tomcat/tomcat.conf':
source => 'puppet:///modules/tomcat/tomcat.conf',
owner => 'tomcat',
group => 'tomcat',
mode => '0644'
}
}
file: modules/tomcat/manifests/config.rb
cookbook_file '/etc/tomcat/tomcat.conf' do
source 'tomcat.conf'
owner 'tomcat'
group 'tomcat'
mode 0644
action :create
end
cookbook_file '/etc/tomcat/tomcat-users.xml' do
source 'tomcat-users.xml'
owner 'tomcat'
group 'tomcat'
mode 0644
action :create
end
file: cookbooks/tomcat/recipes/config.rb
conf
service
refresh
class tomcat::config {
file { '/etc/tomcat/tomcat.conf':
source => 'puppet:///modules/tomcat/tomcat.conf',
owner => 'tomcat',
group => 'tomcat',
mode => '0644',
notify => Service['tomcat']
}
}
file: modules/tomcat/manifests/config.rb
cookbook_file '/etc/tomcat/tomcat.conf' do
source 'tomcat.conf'
owner 'tomcat'
group 'tomcat'
mode 0644
action :create
notifies :restart, 'service[tomcat]', :delayed
end
Update file: cookbooks/tomcat/recipes/config.rb
Note: Add config.rb recipe to default.rb
cookbook_file '/etc/tomcat/tomcat.conf' do
source 'tomcat.conf'
owner 'tomcat'
group 'tomcat'
mode 0644
action :create
notifies :restart, 'service[tomcat]', :delayed
end
cookbook_file '/etc/tomcat/tomcat-users.xml' do
source 'tomcat-users.xml'
owner 'tomcat'
group 'tomcat'
mode 0644
action :create
notifies :restart, 'service[tomcat]', :delayed
end
file: cookbooks/tomcat/recipes/config.rb