Now on steroids!
valid for Puppet v 3.8 - 4.x
show all facters
facter
show OS Family facter
facter osfamily
show facters in YAML
facter -y
show facters in JSON
facter -j
get Puppet Enterprise version
facter -p | grep pe_
get Puppet agent All-in-one version
facter -p | grep aio
use fact inside a manifest using 'facter' function
notify { "OS is ${::facts['operatingsystem']}": }
use fact inside manifest directly
notify { "OS is $::operatingsystem": }
get facter Hash
facter system_uptime
{
days => 2,
hours => 70,
seconds => 253058,
uptime => "2 days" }
facter system_uptime.hours 70
get OS major release from manifest
$::operatingsystemmajrelease
Run MCollective as 'peadmin'
sudo -i -u peadmin
check connection to nodes
mco ping
get nodes by Facter name
mco facts timezone
check how many MCO nodes are running
mco puppet count
restart apache service
mco rpc service restart service=httpd
check status of a package
mco package status puppet
list all subcollectives
mco inventory ––list-collectives
disable puppet agent on node
mco puppet disable -I nodename
find all nodes that are x64
mco ping -F architecture=x86_64
find all nodes containing NYC in hostname
mco find ––with-identity /nyc/
find all nodes with nginx class
mco find ––with-class nginx
kick off 'puppet agent -t' on your nodes
mco puppet runall -F osfamily=Redhat 5
this will run puppet agent -t on all nodes where osfamily fact = redhat, 5 concurrent runs
kick off Puppet run on node 'abc.xyz'
mco puppet runonce -l abc.xyz
run command against a subcollective
mco ping -T us_collective
start/stop service on a node
mco service ntp start -I ny14.nyc
show all available MCO plugins
mco plugin doc
show last run statistics for all nodes
mco rpc puppet last_run_summary
mco client logs (on PE Master)
/var/lib/peadmin/.mcollective.d/client.log
print modulepath:
puppet config print modulepath
modules help:
puppet help module
search available modules: (PuppetForge)
puppet module search 'nginx'
install a module
puppet module install author/module
build new module with full skeleton:
puppet module generate author/module
list installed modules:
puppet module list
check for missing module dependencies
puppet module list --tree
build a module release package (.tar.gz)
puppet module build author/module
show modified files of an installed module
puppet module changes
Module paths:
apache/ # main module dir
apache/manifests # manifest code
apache/lib # plugins, ruby code
apache/templates # ERB templates
apache/files # files used in module
apache/tests # usage examples
apache/Modulefile # metadata paths inside a module:
content => template('mysql/my.cnf.erb'),
Template is in: $modulepath/mysql/templates/my.cnf.erb
source => 'puppet:///modules/mysql/my.cnf'
File is in: $modulepath/mysql/files/my.cnf
Augeas binary tool
/opt/puppetlabs/puppet/bin/augtool
show all available lenses
augtool> ls /augeas/load/
set value in a file
augtool> set /files/etc/ssh/sshd_config/PermitrootLogin no
augtool> save
get specific data tree branch
cat /etc/hosts
127.0.0.1 puppetmaster localhost localhost.localdomain ::1 localhost localhost.localdomain localhost6
#managed nodes
192.168.237.148 host1
192.168.237.155 host2
augtool> ls /files/etc/hosts
1/ = (none)
2/ = (none) #comment = managed nodes
3/ = (none)
4/ = (none)
augtool> ls /files/etc/hosts/4
ipaddr = 192.168.237.155
canonical = host2
check Augeas version
ls /augeas/
root = /
context = /files
variables = (none)
version/ = 1.4.0
save = overwrite
span = disable
load/ = (none)
files/ = (none)
Augeas Help
Admin commands:
help - print help
load - (re)load files under /files
quit - exit the program
retrieve - transform tree into text
save - save all pending changes
store - parse text into tree
transform - add a file transform
Read commands:
dump -xml - print a subtree as XML
get - get the value of a node
label - get the label of a node
ls - list children of a node
match - print matches for a path expression
print - print a subtree
errors - show all errors encountered in processing files
span - print position in input file corresponding to tree
Write commands:
clear - clear the value of a node
clearm - clear the value of multiple nodes
ins - insert new node
insert - insert new node (alias of 'ins')
mv - move a subtree
move - move a subtree (alias of 'mv')
cp - copy a subtree
copy - copy a subtree (alias of 'cp')
rename - rename a subtree label
rm - delete nodes and subtrees
set - set the value of a node
setm - set the value of multiple nodes
touch - create a new node
Augeas in manifest
augeas { "sshd_config": changes => [ "set /files/etc/ssh/sshd_config/PermitRootLogin no", ], }
or if making multiple changes to same file
augeas { "sshd_config": context => "/files/etc/ssh/sshd_config", changes => [ "set PermitRootLogin no", "set Disable yes", "rm PermitTTY yes", "ins PermitTunnel no", ], }
4 types of functions
hiera()
hiera_array()
hiera_hash()
hiera_include()
Hiera Array
$ hiera ssh_users ["root", "jeff", "gary", "hunter"]
$ hiera ssh_users.0
root
Hiera Hash:
$ hiera user {"name"=>"kim", "home"=>"/home/kim"}
$ hiera user.name
kim
use Hiera for class assignment in Site.pp
hiera_include()
Hiera config file
/etc/puppetlabs/puppet/hiera.yaml
Hierarchies:
---
:hierarchy:
- "nodes/%{::clientcert}"
- "roles/%{::role}"
- "%{::osfamily}"
- "%{::environment}"
- common
sample hiera xyz.yaml
---
### xyz
classes:
- apache
- apache::vhost
- ntp
owner: joe smith
location: new york
business_unit: billing
Get hiera value for this host:
hiera classes ::hostname=xyz ::environment=production
["apache", "apache::vhost", "ntp"]
run Hiera in debug mode:
hiera classes ::hostname=xyz -d
Use Hiera in manifest:
$my_dns_servers = hiera("dns_servers")
or array:
$my_dns_servers = hiera_array("dns_servers")
get Hiera key value directly from command line
puppet apply -e "notice(hiera('<KEY>'))"
dfdf
Log File locations:
https://docs.puppet.com/pe/latest/install_what_and_where.html
cron examples
run script every day at 12.30am
cron { 'myscript':
command => '/usr/bin/python myscript.py',
user => 'root',
minute => '30',
hour => '0',
}
run script every tue at 5 am
cron { 'myscript':
command => '/usr/bin/python myscript.py',
user => 'root',
minute => '0',
hour => '5',
weekday => 'Tuesday'
}
404 error, could not request certificate when running
404 puppet run error, could not request certificate
# Apply catalog using -verbose, -ignorecache, -no-daemonize -detailed-exit-codes, -show_diff
puppet agent --test (or -t)
Run in debug mode with more output:
puppet agent -t --debug
Run simulation (no changes to system):
puppet agent -t --noop
Run catalog for different environment than whats configured in Puppet Agent's conf file:
puppet agent -t --environment development
Disable puppet agent from running on node:
puppet agent --disable (this creates a lock file)
to enable:
puppet agent --enable
show all installed packages:
puppet resource package
install or remove package:
puppet resource package apache ensure=present/absent
show all managed resources:
puppet resource
show all Classes that are applied to the node from the compiled catalog
puppet config print classfile
cat /opt/puppetlabs/puppet/cache/state/classes.txt
show all Resources that are applied to the node from the compiled catalog
puppet config print resourcefile
cat /opt/puppetlabs/puppet/cache/state/resources.txt
show a graph of all dependencies found in a catalog, stored in a .dot image
puppet agent -t --graph
## show location of the generated .dot file
puppet agent --configprint graphdir
Certs
List all certs
puppet cert list --all
sign certificate
puppet cert sign 'certname'
remove client cert
puppet cert clean 'certname'
apply config directly from command line and pass parameter
puppet apply -e "class { 'my_super_module': version => '1.2'}"
puppet apply -e "include myClass"
puppet apply -e "user { 'spiderman': ensure => 'present', managehome => true, groups => ['wheel'], home => '/home/spiderman', password=> 'webz', password_max_age => '99999', password_min_age => '0', shell=> '/bin/bash', uid=> '7021',}"
Configuration
Puppet configuration file:
/etc/puppetlabs/puppet/puppet.conf
Print all configuration settings:
puppet config print all
Check current Puppet version and if theres an update available:
./opt/puppetlabs/bin/puppet-enterprise-version-check
Log and File Locations
Puppet Server log:
/var/log/puppetlabs/puppetserver/puppetserver.log
PuppetDB log:
/var/log/puppetlabs/puppetdb/puppetdb.log
SSL certs location:
/etc/puppetlabs/puppet/ssl
Master Configuration file:
/etc/puppetlabs/puppet/puppet.conf
Agent installation packages:
/opt/puppetlabs/server/data/packages/public/current
Site.pp location:
/etc/puppetlabs/code/environments/productions/manifests/site.pp
Environment config file: /etc/puppetlabs/code/environments/production/environment.conf
Troubleshooting Catalog Compile
Debug compilation for specific node
puppet master ––debug ––compile node.xyz
Metrics & Dashboard
https://<PuppetMasterHostname>:8140/puppet/experimental/dashboard.html
Resources:
package { 'ntp': ensure => installed, }
service { 'ntp': name => $service_name, ensure => running, enable => true, subscribe => File['ntp.conf'], }
file { 'ntp.conf': path => '/etc/ntp.conf', ensure => file, require => Package['ntp'], source => "puppet:///modules/ntp/ntp.conf", }
This source file would be located on the Puppet master at
/etc/puppetlabs/code/modules/ntp/files/ntp.conf }
Virtual Resource:
@user { 'www-user': ensure => present, tag => web, }
realize Virtual resource
realize User['www-user'] User <| tag == web |> # Resource default setting:
Resource Default:
Exec { path => '/sbin:/bin:/usr/sbin:/usr/bin', }
this will make all Execs use the default path
list all Puppet resources:
puppet describe ––list
list metaparameters for specific resource
puppet describe user
Case Statement:
case $operatingsystem { centos, redhat: { $service_name = 'ntpd' } debian, ubuntu: { $service_name = 'ntp' } default: { $service_name = 'unknown' } }
If Else Statement:
if $is_virtual { warning('target is virtual.') } elsif $operatingsystem == 'Darwin' { warning('module doesnt work on this OS.') } else { include ntp }
OR, AND
if ($color == 'red') or ($sound == 'quiet') { notify {'should never see this': } }
if ($color == 'red') and ($blah == "ok") {
Selector Statement:
$rootgroup = $osfamily ? { 'Solaris' => 'wheel', /(Darwin|FreeBSD)/ => 'wheel', default => 'root', }
Unless Statement:
unless $memorysize > 1024 { $maxclient = 500 }
IN operator:
if '64' in $::architecture if $user in [ 'joe','fred','bill' ]
PICK function:
chooses the first available option if variable is set, if not, chooses the default (last variable)
** Requires 'puppetlabs/stdlib' module
$root_home = hiera('root_home')
$homedir = pick($root_home,'/root') # if $room_home is not set, use '/root'
Variables:
Assignment to Var:
$content = "some content\n"
Arrays:
$address = [$addr1, $addr2, $addr3]
file {'/tmp/testing': ensure => file, content => $content, }
Iterations
$binaries = ["facter", "hiera", "mco", "puppet", "puppetserver"] # function call with lambda: $binaries.each |String $binary| { file {"/usr/bin/$binary": ensure => link, target => "/opt/puppetlabs/bin/$binary", } }
Hashes:
$warning_msg = { memory => "memory low", disk => "disk space low" }
notify { $warning_msg[disk]: }
complex Hash
$services = { "apache" => { "version" => "2.8", "desc" => "web server" }, "mysql" => { "version" => "5.6", "desc" => "web server" }}
Iterate a complex Hash
$services.each |$name, $value| { notice $name notice $value['version'] }
Resource Ordering:
package { 'openssh-server': ensure => present, before => File['/etc/ssh/sshd_config'], }
file { '/etc/ssh/sshd_config': ensure => file, mode => '0600', source => 'puppet:///modules/sshd/sshd_config', require => Package['openssh-server'], }
same as:
file { '/etc/ssh/sshd_config': ensure => file, mode => '0600', source => 'puppet:///modules/sshd/sshd_config', notify => Service['sshd'], } service { 'sshd': ensure => running, enable => true, subscribe => File['/etc/ssh/sshd_config'], }
Chaining Arrows:
Package['ntp'] -> File['/etc/ntp.conf'] ~> Service['ntpd']
# first: package { 'openssh-server': ensure => present, } -> # and then: file { '/etc/ssh/sshd_config': ensure => file, mode => '0600', source => 'puppet:///modules/sshd/sshd_config', } ~> # and then: service { 'sshd': ensure => running, enable => true, }
or
Service['httpd'] <~ File['apache.conf'] <- Package['httpd']
Service 'httpd' is subscribed to File 'apache.conf', if File changes,
Service will restart. File 'apache.conf' requires Package 'httpd'
Resource Collector Chaining:
Yumrepo <| |> -> Package <| |>
Defined Types:
Before declaring Apache class
define apache::virtualhost ( $ensure = present, $template = 'apache/virtualhost.conf.erb' , [...] ) {file { "ApacheVirtualHost_${name}": ensure => $ensure, content => template("${template}"), }} Declaration of a define, after declaring Apache class
class apache { apache::virtualhost { 'www.myvhost.com': template => 'site/apache/www.myvhost.com-erb' }}
another Def Type example,
$scripts = [ 'break.sh', 'apache.sh', 'startup.py' ] define configure_scripts { file { "/tmp/${name}": ensure => file, source => "puppet:///modules/mymodule/${name}", mode => '0755', noop => false, } } configure_scripts { $scripts:; }
Passing parameters:
<%= @fqdn %> For loop <% @dns_servers.each do |ns| %> nameserver <%= ns %> <% end %> <% for @item in @shopping_list %> <%= @item %> <% end %>
If else
<% if @param -%>blah blah<% elsif @param == 'duck' -%> this is a duck <% else %>xxxx
<% end %>
if and or
<% if @param1 == 'a' or $param1 == 'b' %>
PE 2016.2 EL7 x64 tarball https://s3.amazonaws.com/pe-builds/released/2016.1.2/puppet-enterprise-2016.1.2-el-7-x86_64.tar.gz
Puppet Master: 8140
Certificate Auth: 8140
Orchestration: 8142
PE Console: 443
Console Services: 4433
PuppetDB: 8081
PostgresSQL: 5432
Mcollective: 61613
ActiveMQ: 61616
Max # of nodes per instance (conservative estimate):
PE Master: 700-800
ActiveMQ Broker: 800