using puppet to pull strings - darksim · using puppet to pull strings: ... what is puppet and why...

159
Using Puppet to Pull Strings: a Gentle Introduction to Puppet Thomas Uphill http://ramblings.narrabilis.com

Upload: ngotram

Post on 28-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Using Puppet to Pull Strings:a Gentle Introduction to Puppet

Thomas Uphill

http://ramblings.narrabilis.com

Page 2: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

http://goo.gl/FZvBD

example fileshttp://ramblings.narrabilis.com/Talks/puppet-picc.tar.bz2

Latest Versionon Google Docs

Page 3: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Overview

What is Puppet and why do I want it?

Puppet 101Introductory Material

Puppet 102Advanced Material

Questions sporadically throughout.

Page 4: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

What is puppet?

Think of it as a language.

Describe state, not steps.

Paint a picture of your ideal and most clean system.

Puppet does the rest!

Page 5: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppetlabs

● Luke Kanies in 2005● http://puppetlabs.com/● puppet Enterprise

○ commercial support○ windows support○ puppet forge

Page 6: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Why do I want it?

you can do cool things with itHuman-parsable.ensure that all your hosts are running ssh

service {"ssh": ensure => running;}

Page 7: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Puppet 101Resource Abstraction LayerSyntaxtrifectacore types(file, package, service)puppet apply / puppet resourcefacter/variablesconditionalsbooleans/arithmeticin

templatestypes II(augeas, exec, cron)classesfunctionsfilebuckets

Page 8: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

versions

0.20 0.25 2.6.0 2.7.0

2.7.21

0.20 0.25 2.6.0 2.7.0

2.7.21

3.03.0

3.1.13.1.1

Page 9: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

learning puppet vm

http://docs.puppetlabs.com/learning/

VMwareOVFVirtualBoxKVM

Get the Learning Puppet VM

Page 10: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

learning puppet vm

KVM

[root@hv] vmware-mount -f learn_puppet.vmdk /mnt[root@hv] qemu-img /mnt/flat -Oqcow2 learn_puppet.qcow2

Page 11: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Resource Abstraction Layer

types providers

ubuntu

SLES

macosx

Windows

user

file

package

service

exec

mount

group

host

RHELCENTOS

Springdale

Page 12: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

manifest

pluginmodule

Language (preview)

classattributetypenode

factvariable

class class

Page 13: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Syntax

type { "title":

attribute => value,

attribute => value

}

Page 14: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Syntax

type { "title":

attribute => value,

attribute => value

}

syntax.pp

file {"testfile": mode => 0644, owner => root path => "/etc/test", ensure => present}

[root@learn ~] # puppet parser validate syntax.pp err: Could not parse for environment production: Syntax error at 'path'; expected '}' at /etc/puppetlabs/puppet/manifests/syntax.pp:4err: Try 'puppet help parser validate' for usage

Page 15: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

trifecta

file

package

service

Page 16: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

core types cheat sheet

docs.puppetlabs.com/puppet_core_types_cheatsheet.pdf

Page 17: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

fileAttributes

ensure

pathtitle: source

content

target

recurse

purge

owner

group

mode

Page 18: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

file {"issue":path => "/etc/motd",content => "Hello World!"}

/etc/motd

Hello World!

distributing file contents

Page 19: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

distributing file contents 2

Attributes

target

recursefile {"issue":path => "/etc/motd",content => file("/etc/hello")}

/etc/motd

Hello File!

/etc/hello

Hello File!

Page 20: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

distributing file contents 3

file {"issue":path => "/etc/motd",content => template("/etc/hello.erb")}

/etc/motd

Hello learn!

/etc/hello.erb

Hello <%= hostname %>!

Page 21: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppet apply (serverless operation)

apply changes using a locally defined manifest

[root@learn ~] # puppet apply mymanifest.pp

Page 22: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

distributing file contents 4

Attributes

file {"issue":path => "/etc/motd",source =>"puppet:///files/hello"}

/etc/motd

Hello puppet!

/etc/puppet/manifests/hello

Hello puppet!

Page 23: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

packageAttributes

ensure

nametitle: source

Page 24: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

henson.local

demo VMs

learn jim

kermitpiggy

Page 25: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

serviceAttributes

ensure

nametitle: enable

hasrestarthasstatusrestartstatus

Page 26: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

ordering/dependencies

file

package

service

host.conf httpd

httpd

Page 27: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

DAG

host.conf httpd

httpd

directed acyclic graph

DirectedAcyclicGraph

Page 28: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Referencing objects - case

Defining a new object uses lower case:

Referencing an existing object uses upper case:

Be careful not to define objects twice!

service["sshd"] { ensure => running }

require => Service["sshd"]

Page 29: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

/etc/puppet/lopsa/ordering.pp

package {'httpd': ensure => present}service {"httpd": enable => true, ensure => running, hasrestart => true, hasstatus => true, require => [Package['httpd'],File['host.conf']]}

file {"host.conf": path => "/etc/httpd/conf.d/$name", mode => 0644, owner => 'apache', group => 'apache', content => "<VirtualHost *:80> ServerName kermit.henson.local DocumentRoot /var/www/html/kermit </Virtualhost>", require => Package['httpd']}

ordering/dependencies

Page 30: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppet resource

[root@learn: ~] $ puppet resource --typesaugeascomputercronexecfilefilebucketgroup...

[root@learn: ~] $ puppet resource mount swapmount { 'swap': ensure => 'unmounted', device => '/dev/vg00/swapvol', dump => '0', fstype => 'swap', options => 'defaults', pass => '0', target => '/etc/fstab',}

Page 31: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

memory

Facter/Facts

processors

ipaddress

architecture

dns

VM?

selinux

timezonesystem

$processor0$ipaddress$architecture$fqdn$is_virtual$memorysize$selinux$timezone

Page 32: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

variables

use as parametersuse in conditionalsfactsuser defined

$processor0

$myvar = "My Variable"

$::processor0

Page 33: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

conditionals

● if/else/unless● case● selector

● booleans

Page 34: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

booleans

$x = 1$y = 2($x == $y)($x > $y)($x < $y)($x != $y)($x < $y) and !($x < $y)

false

false

true

true

false

Page 35: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

arithmetic

$x=1$y=2$x+$x == $y$x-$x$y/2$y >> 1$y << 1

true

1

1

4

0

Page 36: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

if/else/unlessif $myvar == "My Variable" {file {'/my/variable':

content => "All is good", ensure => present

}} elsif $myvar {

file {'/my/variable':content => $myvar,ensure => present

}} else {

file {'/my/variable':content => "bad var",ensure => present

}}

$variablefile = "/my/variable"if $myvar == "My Variable" {file {$variablefile:

content => "All is good", ensure => present

}} elsif $myvar {

file {$variablefile:content => $myvar,ensure => present

}} else {

file {$variablefile:content => "bad var",ensure => present

}}

Page 37: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

if/else/unlessunless

not yet implementedif !(condition)

if !($myvar) {file {"/my/file":

content => "no myvar set",ensure => present

}}

Page 38: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

casecase $::hostname {

/^ldap/: { include ldapserver }/^www/: { include webserver }/^dns/: { include bind }/^mx[12]/: { include mxprimary }/^mx[3-9]/: { include mxsecondary }default: { include base }

}

Regular Expressions

match =~if $::hostname =~ /^ldap/ {

include ldapserver}

not match !~if $::hostname !~ /test/ {

include production}

Page 39: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

casecase $::hostname {

/^ldap/: { include ldapserver }/^www/: { include webserver }/^dns/: { include bind }/^mx[12]/: { include mxprimary }/^mx[3-9]/: { include mxsecondary }default: { include base }

}

Regular Expressions● capture

/^ldap(\d+)/ { include "ldap$1" }

Page 40: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

selectorternary operator (C)x == y ? "they are equal" : "they are different"

attribute => $fact ? {'value' => 'result','other_value' => 'other_result',default => 'default_result'

}

Page 41: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

"in"if $::hostname in ['www','web'] {

service {'httpd':ensure => true

}}

if $::kernelversion in ['2.6.35-22','2.6.38.6-26.rc1.fc15'] {

service {'sshd':ensure => false

}}

Page 42: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

backus naur (bnf)<exp> ::= <exp> <arithop> <exp> | <exp> <boolop> <exp> | <exp> <compop> <exp> | <exp> <matchop> <regex> | ! <exp> | - <exp> | "(" <exp> ")" | <rightvalue>

<arithop> ::= "+" | "-" | "/" | "*" | "<<" | ">>"<boolop> ::= "and" | "or"<compop> ::= "==" | "!=" | ">" | ">=" | "<=" | "<"<matchop> ::= "=~" | "!~"

<rightvalue> ::= <variable> | <function-call> | <literals><literals> ::= <float> | <integer> | <hex-integer> | \

<octal-integer> | <quoted-string><regex> ::= '/regex/'

http://docs.puppetlabs.com/guides/language_guide.html

Page 43: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

templates

ERB syntax<% Ruby code %>

<%= Ruby expression %><%# comment %>

-%> no newline<%% replace with <%%%> replace with %>

Page 44: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

templates (conditionals)

puppet ERB<%= @fact %> replace with fact/global variable

conditionalclient.conf.erb<% if @ipaddress_eth0 != "NONE" %>ServerName <%= @printserver %><% end %>

Page 45: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

templates (iteration)

iteration$nameservers = [ 'ns1.example.com',

'ns2.example.com','ns3.example.com']

$searchdomains = [ 'inside.example.com','outside.example.com','under.example.com']

file {"resolvconf":path => "/etc/resolv.conf",mode => 0644, owner => root, group => root,content => template('resolvconf.erb')

}

iteration$nameservers = [ 'ns1.example.com',

'ns2.example.com','ns3.example.com']

$searchdomains = [ 'inside.example.com','outside.example.com','under.example.com']

file {"resolvconf":path => "/etc/resolv.conf",mode => 0644, owner => root, group => root,content => template('resolvconf.erb')

}

Page 46: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

templates (concatenation)

iteration$nameservers = [ 'ns1.example.com',

'ns2.example.com','ns3.example.com']

$searchdomains = [ 'inside.example.com','outside.example.com','under.example.com']

file {"resolvconf":path => "/etc/resolv.conf",mode => 0644, owner => root, group => root,content => template('header.erb','resolvconf.erb')

}

Page 47: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

types II

cronexecaugeas

Page 48: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

cronAttributes

command

hourname: minute

monthmonthday

user

weekday

Page 49: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

execAttributes

command

createstitle: cwd

environmentgrouplogoutput

onlyif

path

refreshrefreshonlyreturnsunless

Page 50: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

tool to transform files into objectsconfig changes made to the objectsaugtool and augparse command line toolsWhy?

● manipulate parts of a file● don't clobber another modules work● start from a known good config

Page 51: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

augeasAttributes

changes

contexttitle: force

nameonlyif

Page 52: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

augeas

sshd_config

augeas{ "sshd password authentication": context => "/files/etc/ssh/sshd_config", changes => [ "set PasswordAuthentication no" ], notify => Service["sshd"]}

Page 53: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

class

classes

attributetype

attributetype variable

variable

classinclude otherclass

Page 54: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

class syntax

class name {type {title: attributes => values },type {title: attributes => values }

}

Page 55: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

class hierarchy

parent nephew

top/global

child

Page 56: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

functions

includefilejoinsplitdefinedrequire

templaterealizenotice

http://docs.puppetlabs.com/references/stable/function.html

Page 57: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

inline_template

● apply template to variables ● pass to a parameter# iterate over the interfaces# print out their addresses into ifcfg-files$ifs = split($interfaces,',')

define inline_template_test { file {"/tmp/ifcfg-$name": content => inline_template("IPADDR=<%= @ipaddress_${name} %>\n") }}

inline_template_test { $ifs: }/tmp/ifcfg-eth0IPADDR=192.168.122.4

Page 58: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

filebuckets

Defaults to local-only modeServer mode:filebucket { puppet: server => "puppet.example.edu" }

Command-line utility:# puppet filebucket --local backup /etc/puppet/puppet.conf /etc/puppet/puppet.conf: be50b3e9acc2c2de8df194b1466fd2c1

# puppet filebucket --local restore /etc/puppet/puppet.conf \be50b3e9acc2c2de8df194b1466fd2c1

Page 59: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

vim

.vimrc

" puppet style guide modefiletype plugin indent on

Page 60: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Puppet 102advanced languageserver configuration

Page 61: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

plusignmentscopeparameterized classesrun stagesresource chainingpuppetmaster (server operation)webrickfileserverclient configurationdebuggingpuppetcasite.pptype defaultsreferencing/upper case

puppet 102 modulespluginscustom factscustom typespluginsyncpassengerpushingreportingencenvironmentsstored configurationsvirtualizing resourcesexported resourcessystem integration/firewallnagios

Page 62: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

plusignment

class openssh {

file { "/etc/ssh/sshd_config":

content => "..."; }

service {

"sshd":

ensure => running,

subscribe => File["/etc/ssh/sshd_config"]; }

class keydistribute {

file {

"/etc/ssh/host_rsa_key":

content => "...";

"/etc/ssh/host_dsa_key":

content => "..."; }

Service["sshd"] {

subscribe +> [ File["/etc/ssh/host_rsa_key"],

File["/etc/ssh/host_dsa_key"] ] }

}

}

Page 63: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

scope

class base

hostname

class dns

$::hostname

class dns

class base

hostname

Page 64: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

scopeinclude parent::child$var = "top level"class parent { $var = "from parent" $hostname = "hostname from parent"}class parent::child inherits parent { $var = "from parent::child" notice ( "parent::var => ",$parent::var ) notice ( "var => ",$var ) notice ( "hostname => ",$hostname ) notice ( "::hostname => ",$::hostname )}

http://docs.puppetlabs.com/guides/scope_and_puppet.html

Page 65: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

parameterized classes# unparameterizedinclude resolverclass resolver { $dnsservers = ['8.8.8.8','8.8.4.4'] file {"resolver-conf": path => "/tmp/resolv.conf", content => template("/root/resolver-conf.erb")

}}

Page 66: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

parameterized classes# parameterizedclass {"resolver": dnsservers => ['192.168.1.1','192.168.4.1']}class resolver ($dnsservers =['8.8.8.8','8.8.4.4']) { file {"resolver-conf": path => "/tmp/resolv.conf", content => template("/root/resolver-conf.erb")

}}

Page 67: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

parameterized classes# parameterized2

class resolver ($mydnsservers = '') {

$maindnsservers = '8.8.8.8,8.8.4.4'

if $mydnsservers {

$dnsservers = split(sprintf("%s,%s",$mydnsservers,$maindnsservers),",")

} else {

$dnsservers = split($maindnsservers,",")

}

file {'resolver-conf':

path => '/tmp/resolv.conf',

content => template('/etc/picc/resolver-conf.erb')

}

}

class {'resolver':

mydnsservers => "192.168.1.1,192.168.4.1"

}

Page 68: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

custom definesMove beyond the singletons of typical puppet manifests.

Defines are used to model repeatable chunks of puppet code.

Common examples include versioning repositories and apache virtual hosts.

Not much to explain, let's just take a look at an example...

Page 69: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

class git { package{ "git": } file {'/var/lib/git': ensure => 'directory', mode => 0755, owner => 0, group => 0 }}

define gitrepo($owner, $group, $ispublic = false) { include git $mode = $ispublic ? { true => 2774, false => 2770 }

file { "/var/lib/git/${title}": ensure => directory, owner => "${owner}", group => "${group}", mode => $mode, require => [Package['git'],File['/var/lib/git']] }

exec { "${title}_initialize": command => '/usr/bin/git --bare init .', cwd => "/var/lib/git/${title}", unless => "/bin/test -f /var/lib/git/${title}/HEAD", require => [Packages['git'],File["/var/lib/git/${title}"]] }}

Page 70: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

"chain" operatorsClass["setup_repos"] -> Class["install_packages"] -> Class["ldap::client"]

Package["openssh-server"] -> File["/etc/ssh/sshd_config"] -> Service["sshd"] <- File["/etc/ssh/ssh_host_rsa_key.pub"]

Run stages & resource chaining

setup_repos install_packages ldap::client

before => Class['install_packages', 'ldap::client']

before => Class['ldap::client']require => Class['setup_repos']

require => Class['setup_repos', 'install_packages']

Page 71: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Run stages - the parameterized waystage {

"pre":before => Stage["main"];

"post":require => Stage["main"];

}

node "server.example.com" {class {

"setup_repos":stage => "pre";

"install_packages":stage => "main";

"ldap::client":stage => "post";

}}

Page 72: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppetmaster

Allows all puppet work to be done in a single location and then distributed via a pull-based puppet client.

puppetmaster

Webserver

DHCPserver

DNSserver

Databaseserver

TCPport 8140

SSLx509

Page 73: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppetmaster

● Pairs well with a versioning system such as git or svn to create living manifests.

● Options controlled through [master] section in puppet.conf

● "puppet-server" package. Creates user/group puppet.

● Simplest case is webrick, works out of the box.

Page 74: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

webrick

Works without any additional configuration

service puppetmaster start

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8140 -j ACCEPT

[root@jim ~] # lsof -i |grep 8140puppetmas 3211 puppet 6u IPv4 17188 0t0 TCP *:8140 (LISTEN)

Page 75: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppetmaster as a fileserver

If you use "source => puppet:///" in any of your file definitions, this is how those files get distributed.

/etc/puppet/fileserver.conf

[files]path /var/lib/puppet/filesallow *.example.comallow *.friendly.example.orgdeny *.malice.example.netdeny impostor.friendly.example.org

Page 76: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppet.conf

auth.conf

fileserver.conf

manifests/site.pp

server configuration

Page 77: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

server configuration/etc/puppet/puppet.conf

[main]logdir = /var/log/puppetrundir = /var/run/puppetssldir = $vardir/ssl

[agent]classfile = $vardir/classes.txtlocalconfig = $vardir/localconfig

Page 78: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

server configuration

/etc/puppet/fileserver.conf

[files]path /var/lib/puppet/filesallow *.example.com

[modules]allow *.example.com

[facts]path /var/lib/puppet/factsallow *.example.com

[plugins]path /var/lib/puppet/pluginsallow *.example.com

Page 79: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

client configuration

/etc/sysconfig/puppet

PUPPET_SERVER=puppet.example.com#PUPPET_PORT#PUPPET_LOG#PUPPET_EXTRA_OPTS=--waitforcert=500

/etc/puppet/puppet.conf

[agent]server = puppet.example.com

Page 80: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

it's go time.

on the master:service puppetmaster start

on the client:service puppet start

...hope for the best.

Page 81: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

an aside...debugging clients

[root@client ~] # service puppet stop

Run and print the log output to the shell:[root@client ~] # puppet agent -o --no-daemonize -vor[root@client ~] # puppet agent -t

View even more information on the shell:[root@client ~] # puppet agent -o --no-daemonize --debug

View a crazy, incomprehensible amount of info on the shell:[root@client ~] # puppet agent -o --no-daemonize --trace

Page 82: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

info: Creating a new SSL key for kermit.henson.localwarning: peer certificate won't be verified in this SSL sessioninfo: Caching certificate for cawarning: peer certificate won't be verified in this SSL sessionwarning: peer certificate won't be verified in this SSL sessioninfo: Creating a new SSL certificate request for kermit.henson.localinfo: Certificate Request fingerprint (md5): F4:E2:10:61:1C:F7:F8:E4:03:98:14:B0:F7:47:C8:9Ewarning: peer certificate won't be verified in this SSL sessionwarning: peer certificate won't be verified in this SSL sessionwarning: peer certificate won't be verified in this SSL sessionExiting; no certificate found and waitforcert is disabled

puppetca

Page 83: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

puppetca

Distributes and manages x509 certificates.

Command line utility: puppet cert (puppetca for oldtimers)

Supports auto-signing

/etc/puppet/autosign.conf

*.example.com*.subdomain.example.net

Page 84: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

it's go time

server[root@jim puppet]# puppet cert list

kermit.henson.local (F4:E2:10:61:1C:F7:F8:E4:03:98:14:B0:F7:47:C8:9E)

[root@jim puppet]# puppet cert sign kermit.henson.local

notice: Signed certificate request for kermit.henson.local

notice: Removing file Puppet::SSL::CertificateRequest kermit.henson.local at '/var/lib/puppet/ssl/ca/requests/kermit.henson.local.pem'

Page 85: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

it's go time[root@kermit ~]# puppet agent -v --no-daemonize -o

warning: peer certificate won't be verified in this SSL session

info: Caching certificate for kermit.henson.local

info: Caching certificate_revocation_list for ca

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find default node or by name with 'kermit.henson.local, kermit.henson, kermit' on node kermit.henson.local

notice: Using cached catalog

err: Could not retrieve catalog; skipping run

[root@enchantress ~]#

Page 86: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

site.pp

master manifestneeds to define each node or a default node

simplest* site.ppnode default {}

Page 87: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

site.pp

import 'nodes.pp'import 'functions.pp'import 'variables.pp'import 'defaults.pp'

Page 88: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

type defaults (defaults.pp)capitalize the type: ...but do not give a title

These "attribute => value" pairs will now be the defaults for all objects of type "file". You are free to overwrite them later.

File {user => root,group => root,mode => 0644

}

Page 89: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

tip: tag template

tagprinter.erb

tags----<% tags.each do |tag| -%><%= tag %><% end -%>

classes-------<% classes.each do |cls| -%><%= cls %><% end -%>

variables-------<% scope.to_hash.keys.each do |var| -%><%= var -%> => <%= scope.lookupvar(var) %><% end -%>

Page 90: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

modules

{modulepath}└── {modulename} └── files └── manifests └── templates

● a way to organize your manifests● promotes code reuse● provides namespaces● can push code to clients

Page 91: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

modules

{modulepath}└── {modulename} └── files └── manifests └── init.pp └── templates

Page 92: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

modules

init.pp

class modulename {file {"example.conf":

path => "/tmp/example.conf",source => "puppet:///modules/modulename/example.

conf"}

}

{modulepath}/{modulename}/files/example.conf

{modulepath}/{modulename}/manifests/init.pp

Page 93: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

modules

subclass.pp

class modulename::subclass {file {"subclass.conf":

path => "/tmp/subclass.conf",source => "puppet:///modules/modulename/subclass.conf"

}}

{modulepath}/{modulename}/files/subclass.conf

{modulepath}/{modulename}/manifests/subclass.pp

Page 94: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

modules

subclass.pp

class modulename::subclass {file {"subclass.conf":

path => "/tmp/subclass.conf",content => template("modulename/subclass.erb")

}}

{modulepath}/{modulename}/templates/subclass.erb

Page 95: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

module cheat sheethttp://docs.puppetlabs.com/module_cheat_sheet.pdf

Page 96: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

style guide

http://docs.puppetlabs.com/guides/style_guide.html

● two-space soft tabs (no literal tab characters)● no trailing white space● 80 character line width● fat comma arrows (=>) within blocks of attributes● # for comments● 'my variable' unless "my ${variable}"● ensure first● defaults in site.pp● separate files for all classes in a module

Page 97: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

forge

http://forge.puppetlabs.com/

> 2.7.14puppet module< 2.7.14puppet-module

Page 98: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

plugins{modulepath}└── {modulename} └── files └── lib └── facter └── puppet └── parser └── functions └── provider └── exec └── package └── anytype └── type └── util

Page 99: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

custom facts

/etc/puppet/modules/videocard/lib/facter/videocard.rb

# Josko Plazonic - lifted from Josko March 14, 2011 by Thomas Uphillrequire 'facter'

Facter.add("videocard") doconfine :kernel => :linuxENV["PATH"]="/bin:/sbin:/usr/bin:/usr/sbin"setcode do

controllers = []lspciexists = system "/bin/bash -c 'which lspci

>&/dev//null'"if $?.exitstatus == 0

output = %x{lspci}output.each {|s|

controllers.push($1) if s =~ /VGA compatible controller: (.*)/

}endcontrollers

endend

Page 100: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

custom types

/etc/puppet/modules/newtype/lib/puppet/type/newtype.rb# this is beyond the context of this talk...# best to look on http://forge.puppetlabs.com/

Puppet::Type.newtype(:newtype)donewproperty(:myprop, :array_matching => :all)doend

end

Page 101: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

pluginsync

plugins synced from master to the clients

/etc/puppet/module

something

libfacter puppet

/var/lib/puppet

libfacter puppet

Page 102: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

pluginsync

videocard example, videocard.rbpuppet.conf -> [agent] pluginsync = true

info: Retrieving pluginnotice: /File[/var/lib/puppet/lib/videocard.rb]/ensure: defined info: Loading downloaded plugin var/lib/puppet/lib/videocard.rb

Page 103: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modules + Facts + TemplatesControl access to system users via cgroups

Filestructure:

/etc/puppet/modules/cgroups└── templates └── cgconfig.conf└── files └── cgrules.conf└── manifests └── init.pp└── lib └── facter └── cgroups.rb

Page 104: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modules + Facts + Templates

manifests/init.pp class cgroups {

package {"libcgroup":

ensure => present;}

service {"cgconfig":

ensure => running,enable => true,require => Package["libcgroup"];

"cgred":ensure => running,enable => true,require => [ Package["libcgroup"], Service["cgconfig"]

];}

file {"/etc/cgconfig.conf":

content => template("cgroups/cgconfig.conf"),notify => [ Service["cgconfig"], Service["cgred"] ];

"/etc/cgrules.conf":source => "puppet:///modules/cgroups/cgrules.conf",notify => [ Service["cgconfig"], Service["cgred"] ];

}}

Page 105: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modules + Facts + Templates

files/cgrules.conf@staff cpu staff/

@student cpu,memory users/@faculty cpu,memory users/@grad cpu,memory users/@guest cpu,memory users/@alumni cpu,memory users/

* cpu system/

Page 106: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modules + Facts + Templates

templates/cgconfig.confgroup staff {

cpu {cpu.shares = 20;

}}

group users {cpu {

cpu.shares = 60;}memory {

memory.limit_in_bytes = <%= mem80pct %>;memory.memsw.limit_in_bytes = <%= memswap80pct %>;

}}

group system {cpu {

cpu.shares = 20;}

Page 107: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modules + Facts + Templates

lib/facter/cgroups.rbFacter.add(:mem80pct) do setcode do Facter::Util::Resolution.exec('\

free -bto | \grep Mem | \awk \'{OFMT = "%.0f"; print $2*.8}\'\

') endend

Facter.add(:memswap80pct) do setcode do Facter::Util::Resolution.exec('\

free -bto | \grep Total | \awk \'{OFMT = "%.0f"; print $2*.8}\'\

') endend

Page 108: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modules + Facts + Templates

lib/facter/cgroups.rbFacter.add(:mem80pct) do setcode do Facter::Util::Resolution.exec('\

free -bto | \grep Mem | \awk \'{OFMT = "%.0f"; print $2*.8}\'\

') endend

Facter.add(:memswap80pct) do setcode do Facter::Util::Resolution.exec('\

free -bto | \grep Total | \awk \'{OFMT = "%.0f"; print $2*.8}\'\

') endend

Page 109: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Production - Use passengerApache a better webserver than webrick, mongrel support is deprecated.

Much more stable.

config.ru - must be owned by puppet

Page 110: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Passenger - Apache configurationLoadModule passenger_module modules/mod_passenger.so<IfModule mod_passenger.c> PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.2 PassengerRuby /usr/bin/ruby</IfModule>RackAutoDetect OffRailsAutoDetect OffListen 8140<VirtualHost *:8140> RequestHeader set X-SSL-Subject %{SSL_CLIENT_S_DN}e RequestHeader set X-Client-DN %{SSL_CLIENT_S_DN}e RequestHeader set X-Client-Verify %{SSL_CLIENT_VERIFY}e DocumentRoot /etc/puppet/rack/public/ RackBaseURI /</VirtualHost>

Page 111: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Performance PassengerPuppet docs recommend the use of the following apache config directives:

# Refresh old puppetmaster processes after 5 minutes...PassengerPoolIdleTime 300# Set to num_clients * 1.15, 15% more than required...PassengerMaxPoolSize 50# Puppetmaster takes a while, just use one global queue...PassengerUseGlobalQueue on# We don't need most of the passenger stuff, disable it...PassengerHighPerformance on

Keep an eye on memory usage!

Page 112: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Puppet pushing

Puppet is strictly pull-based by design.

Cannot truly push changes.

"puppetrunner" allows the master to remotely trigger the agent to run.

/etc/puppet/puppet.conf[agent] listen = true

Page 113: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Reporting

Agent creates a state.yaml file detailing the results of each run. Have the agent send the report to the master:

[agent] report = true

Report plugins parse state.yaml from the agents and sends the data off to some other utility.

[master]reports=<report_format>

Page 114: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Reporting Engines

Common report formats include:● http● log● rrdgraph● tagmail● puppetdashboard (PE Console)● foreman

Providers are located in:${RUBY_LIBDIR}/puppet/reports/*.rb

Page 115: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Puppet dashboard

Page 116: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Foreman

Page 117: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

foreman

Can extend an existing puppet database, removing need for puppet/foreman syncing scripts.

Extensible, can control DHCP, DNS, etc

http://theforeman.org/

Page 118: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

ENC

external node classifier● script● ldap

● classes● top scope variables● environment

Page 119: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

ENC

external node classifier

● execute a script to determine classes to apply to a node.

● script receives fqdn as argument 1● must return yaml:

○ classes○ top scope variables○ environment

Page 120: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

ENC

/usr/bin/puppet_node_classifier#!/usr/bin/ruby

require 'yaml'

# create an empty hash@enc = Hash.new@enc["classes"] = Hash.new@enc["classes"]["base"] = Hash.new@enc["parameters"] = Hash.new@enc["environment"] = 'production'

if ARGV[0] =~ /^www/ @enc["classes"]["webserver"] = Hash.newend

if ARGV[0] =~ /^kermi/ @enc["classes"]["kermit"] = Hash.newendputs @enc.to_yamlexit(0)

Page 121: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

ENC#!/bin/bashset -eset -u

ENC_BASE_URL="https://localhost:443/nodes"

curl -k -H "Accept: text/yaml" "${ENC_BASE_URL}/${1}"

Page 122: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

ENC LDAP[master]

node_terminus = ldapldapserver = ldap.henson.localldapbase = ou=hosts,dc=henson,dc=local

schema:attributeTypes:

puppetclassparentnodeenvironmentpuppetvarpuppetClient

# kermit.henson.local servers, hosts,kermit.henson.localdn: cn=kermit.henson.local,ou=servers,ou=hosts,dc=henson,dc=localcn: kermit.henson.localipHostNumber: 192.168.122.4puppetclass: serverpuppetclass: logpolicypuppetclass: netwatch::serverobjectClass: topobjectClass: iphostobjectClass: puppetclient

Page 123: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

environments

[myenviro]modulepath = $confdir/environments/myenviro/modulesmanifest = $confdir/environments/myenviro/site.ppmanifestdir = $confdir/environments/myenviro/manifeststemplatedir = $confdir/environments/myenviro/templates

create separate workspaces for clients, change the manifests/modules based on the environment setting

Page 124: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

environments[master]

modulepath = $confdir/environments/$environment/modules:$confdir/modules

manifest = $confdir/environments/$environment/manifests/site.pp

[production]

modulespath = $confdir/production/modules

manifest = $confdir/production/manifests/site.pp

[dev]

modulespath = $confdir/test/modules

manifest = $confdir/test/manifests/site.pp

http://docs.puppetlabs.com/guides/environment.html

Page 125: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

environments + git● make new branches

git branch bobsbranch

● ssh-keys from git to puppetmaster

● update environments directory on commit to gitpuppet.git/hooks/post-receive

● tell agent to use the new environmentpuppet agent -t --environment bobsbranch

or/etc/puppet/puppet.conf[agent]environment=bobsbranch

http://docs.puppetlabs.com/guides/environment.html

Page 126: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Stored Configurations

Enables use of more advanced puppet features.

Used as a cache, speeds up catalog creation.

puppet.conf[master]

storeconfigs=truedbadapter=mysqldbname=puppetmasterdbuser=puppetmasterdbpassword=superdupersecretdbserver=localhost

Page 127: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

fact_names

inventory_facts

param_values

param_namesinventory_nodes

resources

hostsfact_values

source_files

puppet_tagsresource_tags

Stored Configurations DB Schema

Page 128: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Stored Configurations DB Schema

Sample entry from "resources" table:id: 11518title: node001.example.comrestype: Sshkeyhost_id: 28source_file_id: 3exported: 1line: 307updated_at: 2012-04-23 15:25:15created_at: 2012-04-23 15:25:15

Page 129: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Virtualizing Resources

Create a resource, but don't actually send it to the host, keep it local to this manifest.

@ operator

@file { ... }@service { ... }@my_custom_type { ... }

Page 130: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

The inverse - resource realization

Ok I changed my mind, send this resource to the host.

<| |> operator, or more recently, the realize() function.

File <| |> realize(File["title"])Service <| |> realize(Service["title"])My_custom_type <| |> realize(My_custom_type["title"])

Page 131: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Realize use case

base.pp:class base {

file {"/tmp/a":

mode => 644;}@user {

"lopsa":ensure => present;

}}

class manifest1 inherits base {

realize(User["lopsa"])...

}

class manifest2 inherits base {

realize(User["lopsa"])...

}

class manifest3 inherits base {

...}

Page 132: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

exported resources cheat sheethttp://ramblings.narrabilis.com/Talks/exported_resources_cheatsheet.pdf

http://goo.gl/8KAsM

Page 133: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Exporting resources

Just sets the "exported" flag in mysql. These resources are now visible to all hosts.

@@ operator

@@file { ... }@@service { ... }@@my_custom_type { ... }

Page 134: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Collecting exported resources

This setup is really helpful for inter-machine communication.

<<| |>> operator

File <<| |>>Service <<| |>>My_custom_type <<| |>>

Page 135: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

See where this is heading?

In base.pp:

@@sshkey { "${::fqdn}": ensure => present, type => "ssh-rsa", key => "${::sshrsakey}", require => Service["sshd"];}

Sshkey <<| |>>

Done! Now, all of your machines know every other machine's SSH key.

The SSH warning, "are you sure you want to continue? (yes/no):" is a thing of the past!

Of course, this only works for machines within the puppet infrastructure.

Page 136: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Exported resources "gotcha"Be careful when picking the title of any resource that's to be exported.

@@file["/etc/motd"] { mode => 644 }

You'd think this would be fine, but you'll get a "cannot redefine" error if more than one host runs it. Instead, do something like:

@@file {"/etc/motd_${::fqdn}":

path => "/etc/motd",mode => 644;

}

Page 137: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Grouping exported resources

Remember the builtin "tag" parameter?

@@sshkey{ "${::fqdn}": key => ${::sshrsakey}, tag => "group1";}

Sshkey <<| tag == "group1" |>>

Page 138: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Multiple group membership

Tags can be set as an array, but collected based on one value.@@sshkey{ "${::fqdn}": key => "${::sshrsakey}", tag => [ "internal", "external" ];}if $::hostname =~ /internal.example.com/ { Sshkey <<| tag == "internal" |>>}else {Sshkey <<| tag == "external" |>>}

Page 139: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Modify-on-collect

Interact with resources that only exist in the local manifest.

service { "sshd": ensure => running;}

Sshkey <<| |>> { notify => Service["sshd"]}

Page 140: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Systems Integration

Using exported resources, one can build a network adaptive to IP address & hostname changes.

Most common cases are system installation and some form of monitoring.

Puppet has builtin types for integrating with nagios. We'll add in bits to handle monitoring system firewall rules.

Page 141: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

simple server/client autoconfig

serverA192.168.1.1

80 192.168.1.2

client

Page 142: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

simple server/client autoconfig

File (tell client where to find the server)client stores config in /etc/service.confclass server {

@@file {"server_$::fqdn":

path => '/etc/service.conf',

content => inline_template('server=<%= ipaddress -%>'),

tag => 'server-config'

}

}

class client {

File <<| tag == 'server-config' |>>

}

Page 143: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

simple server/client autoconfig

Firewall (tell server to allow client)class client {

@@firewall {"80 allow client $::fqdn port 80":

proto => 'tcp',

source => "$::ipaddress",

dport => "80",

action => 'accept',

tag => 'allow-client'

}

}

class server {

Firewall <<| tag == 'allow-client' |>>

}

https://github.com/puppetlabs/puppetlabs-firewall

Page 144: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Integration with anaconda/kickstart%post

chvt 6

/bin/false

count=0

exit=1

while [ $exit -ne 0 ]; do

puppet agent -t \

--server puppetmaster.example.com --waitforcert 60 >/dev/tty6 2>&1

exit=$?

count=$((count + 1))

if [ $count >= 5 ]; then

break

fi

done

if [ $exit == 0 ]; then

chkconfig puppet on

else

read "Puppet didn't run successfully, investigate"

fi

%end

%packages --nobase

@core

puppet

%end

Page 145: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

hiera

● solves the nested if problem● set variables based on facts● hierarchical

Page 146: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Final Q & A

Thanks for attending!

Any questions?

Still awake...?

Page 147: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

NaginatorStarted as a module/plugin. Became so useful, stable, and well-tested, it was cooked straight into base installs.

We'll use this following example as a "final review" of sorts. The example will get more complex the further into the manifest we go.

In the following examples, assume we've already created (or downloaded from the puppet forge!) the modules "apache" and "iptables".

Page 148: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

nagios_host

Attributes

address

aliastitle: check_command

IP Address for Connections

check_period

contactsensurehost_nametarget

Display Addresscheck_ping,check_ssh,etce.g. "24x7","workhours"Who shall we notify?presentabsentFQDN for connections.Filename

Page 149: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

nagios_service

Attributes

check_command

check_intervaltitle: check_period

notification_interval

ensure

host_name

max_check_attempts

target

check_ping,check_ssh,etcChecking frequency"24x7","workhours",etcNotification frequencypresentabsentCorresponds to host definitionFailures before notification occurs.File destination

Page 150: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [1/9]class nagios {

package {"nagios":

ensure => present;"nagios-plugins-all":

ensure => present;}service {

"nagios":ensure => running,enable => true,require => Package["nagios"];

}include apacheinclude apache::sslonly

...

Page 151: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [2/9]file {

# Directory for all of the nagios config files..."/etc/nagios/conf":

ensure => directory,owner => "nagios",group => "nagios",recurse => true,ignore => [ ".svn", ".git", "CVS" ],source => "puppet:///filesroot/nagios/etc/conf",mode => 750,notify => Service["nagios"];

# Apache configuration file for nagios..."/etc/httpd/conf.d/nagios.conf":

require => Package["nagios"],notify => Service["httpd"],content => template("/path/to/file");

...

Page 152: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [3/9]...

# Nagios CGI interface configuration file..."/etc/nagios/cgi.conf":

require => Package["nagios"],notify => Service["nagios"],content => template("/path/to/file");

# main nagios configuration file..."/etc/nagios/nagios.cfg":

require => Package["nagios"],notify => Service["nagios"],content => template("/path/to/file");

}

...

Page 153: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [4/9]# We're going to manage the NRPE config file, but export# it to the clients instead of installing it locally.# This way, we can fill in a template with nagios' IP

address.@@file {

"/etc/nagios/nrpe.cfg":content => template("/path/to/file"),tag => "nagios";

}# While we're at it, lets export a firewall rule.# You might consider sprucing this up in production...@@iptables {

"9995 allow monitor":proto => "any",source => "${::ipaddress}",jump => "ACCEPT",

tag => "monitor";}

...

Page 154: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [5/9]...

# To list all of the nagios definitions here would use# up several more slides for minimal scholastic gain, so# this can be a homework assignment instead!

# Here, we need to create all of the nagios types# we've referenced throughout this example.# This includes:# The "24x7" nagios_timeperiod# The "mycontact" nagios_contact# nagios_command defines (check_ping, check_ssh,

host_notify)

# It's not hard, just refer to the puppet type reference.# Create the objects, fill in the attributes, and you'll# have your own fully-puppetized nagios installation up# and running in no time!

...

Page 155: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [6/9]# We can now collect host & service objects from client

nodes:Nagios_host <<| tag == "nagios" |>>Nagios_service <<| tag == "nagios" |>># Client manifests will have the text "include nagios::

client"class client {

package {"nagios-plugins-all":

ensure => present;"nrpe":

ensure => present;}service {

"nrpe":ensure => running,enable => true,require => Package["nrpe"];

}...

Page 156: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [7/9]# Export our host definition:@@nagios_host {

"${hostname}":ensure => present,address => "${ipaddress}",tag => "nagios",alias => "${fqdn}",target => "/etc/nagios/conf/dynamic_hosts.cfg",check_command => "check_ping!100!500",check_interval => "3",contacts => "mycontact",check_period => "24x7",max_check_attempts => 3,require => File["/etc/nagios/conf"],notify => Service["nagios"];

}

...

Page 157: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [8/9]# Begin to export our service definition(s)# Set defaults first in case we decide to add more later.Nagios_service {

target => "/etc/nagios/conf/dynamic_services.cfg",use => "generic-service",is_volatile => "0",host_name => "${hostname}",contacts => "mycontact",notification_period => "24x7",require => [ Nagios_host["${hostname}"],

File["/etc/nagios/conf"] ], notify => Service["nagios"]

} ...

Page 158: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

Bare minimum nagios manifest [9/9]# Now do the actual service export. Just append another# N definitions here and they'll get auto-added to

nagios.@@nagios_service {

"${hostname}_check_ssh":ensure => present,service_description => "SSH",check_command => "check_ssh",check_period => "24x7",check_interval => "3",retry_interval => "3",max_check_attempts => "3",tag => "nagios",notification_interval => "720";

}# Final order of business, collect the nrpe config file:File <<| tag == "nagios" |>> { notify => Service["nrpe"]

}}

}

Page 159: Using Puppet to Pull Strings - DarkSim · Using Puppet to Pull Strings: ... What is Puppet and why do I want it? Puppet 101 ... windows support puppet forge. Why do I want it?

further reading● pro puppet

James Turnbull , Jeffrey McCunehttp://www.apress.com/9781430230571

● learning puppet - puppetlabshttp://docs.puppetlabs.com/learning/

● types documentation - puppetlabshttp://docs.puppetlabs.com/references/stable/type.html

● puppetlabs pdfshttp://info.puppetlabs.com/download-pdfs.html

● function referencehttp://docs.puppetlabs.com/references/stable/function.html