puppet - wlug · writing custom types programmed in ruby simple ones are simple, harder ones are...
TRANSCRIPT
![Page 1: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/1.jpg)
Puppet
Frank SweetserSenior Network EngineerWPI Network Operations and Security
Configuring your systems so you don't have to...
![Page 2: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/2.jpg)
Typical System Lifecycle
Installation
![Page 3: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/3.jpg)
Typical System Lifecycle
InstallationInitial
Configuration
![Page 4: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/4.jpg)
Typical System Lifecycle
InstallationInitial
Configuration
FixesUpdatesAudits
![Page 5: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/5.jpg)
The Problem
● Systems need to be configured● The more systems you have, the more work it is
to configure them all● Bad configuration can be worse than no
configuration● How do you make sure all of your systems
– Get properly configured?– Stay that way?
![Page 6: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/6.jpg)
Manual System Configuration
● Just log in and do it!● Fine for very small number of systems – a very small
number● Attempting to scale brings severe risk of carpal tunnel● Checklists can help... a little● Settings you care about buried with everything else● Missing:
– Auditing– History– Reliable documentation
![Page 7: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/7.jpg)
Install Time AutoConfiguration
● Kickstart (RedHat), preseed (Debian), Jumpstart (Solaris), etc
● Ensures new systems are brought into proper state as part of installation process
● Then what?– Changes in configuration policy– Configs derived from dynamic data– Validation that settings remain correct– Multiple operating systems with varying install
support
![Page 8: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/8.jpg)
SSH Keys and Shell Scripting
– Concurrent safe– Testable– Idempotent– Reversible
– Legible– Full of good
logging– Portable
● Great for adhoc or one off items● Can be pushed out via cron● But do you always write scripts that are:
![Page 9: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/9.jpg)
Or ...
Why bother to go through the trouble of carefully writing scripts that are all those things when you could use someone else's?
![Page 10: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/10.jpg)
Puppet
● Guaranteed to be everything on that list of good stuff
● Written in Ruby● Extensible● Client defaults to polling server for latest config
every 30 minutes● Can listen to have push updates triggered● Includes builtin file server
![Page 11: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/11.jpg)
Puppet Lifecycle
InstallationInitial
Configuration
FixesUpdatesAudits
Puppet
![Page 12: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/12.jpg)
Puppet Components
Puppetmaster
Config files
Puppet Puppet Puppet
Config cacheClient facts
![Page 13: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/13.jpg)
Configuration Elements
● Types● Classes● Nodes● Templates● Defines
![Page 14: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/14.jpg)
Types
● A Type is a particular element that Puppet knows how to configure– Files (content, permissions, ownership)– Packages (ensure installed or absent)– Services (enabled/disabled, running/stopped)– Exec (run commands)
![Page 15: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/15.jpg)
Example: Managing sudoers File
file { “/etc/sudoers”:
ensure => file,
owner => root,
group => root,
mode => 600,
source => “puppet://server/files/sudoer”
}
![Page 16: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/16.jpg)
Native Types Library
● Cron● Exec● File● Filebucket● Group● Host● Mount● Notify● Package
● Resources● Schedule● Service● Sshkey● Tidy● User● Yumrepo● Zone
![Page 17: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/17.jpg)
Filebucket
● Special resource for backing up changed files● Provides complete record of all changes● Can have multiple filebuckets
file { “/etc/sudoers”:
...
backup => backupbucket
}
![Page 18: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/18.jpg)
Classes
● A named collection of type objects● Can include or inherit from other classesclass sudo_class {
include foo_class
file { “/etc/sudoers”:
...
}
package{ “sudo”:
...
}
}
![Page 19: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/19.jpg)
Class Inheritance
class afile {
file { “/tmp/foo”:
ensure => file
source => “/src/versionA”
}
}
class another_file inherits afile {
File[“/tmp/foo”] {
source => “/src/versionB”
}
}
![Page 20: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/20.jpg)
Nodes
● A configuration block matching a client● Can contain types, classes● “default” node matches any client without a node
block
node “erwin.wpi.edu” {
include sudo_class
include other_class
}
![Page 21: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/21.jpg)
External Nodes
● Node definitions can be defined outside of puppet● Script definition
– Takes in node name– Returns node configuration
● Node configuration in YAML format– List of node specific variables– List of classes to include
● Ideal for sites with too many nodes to bother precreating
![Page 22: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/22.jpg)
Dependencies
● “require” and “before” settings ensures that types are applied in the correct order
file { “/etc/sudoers”:
...
require => Package[sudo]
}
package { “sudo”:
ensure => present,
before => File[“/etc/sudoers”]
}
![Page 23: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/23.jpg)
Subscribes
● “notify” and “subscribe” settings can trigger cascaded updates
● Particularly useful in services, execfile { “/etc/ssh/sshd_conf”:
...
notify => Service[“sshd”]
}
service { “sshd”:
subscribe => File[“/etc/ssh/sshd_conf”
}
![Page 24: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/24.jpg)
Notifications
● Results can be sent to different log sources– syslog, email, YAML dump, graphs
● Custom report methods are possible● Simplest is email
From: report@puppethost
Subject: Puppet Report for clienthost
Tue Nov 13 16:52:42 0500 //clienthost/server_base/File[/etc/syslog.conf]/ensure (notice): created
![Page 25: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/25.jpg)
Templates
● Apply code and variable substitution● Can be applied to files or strings● Uses ERB, same templating engine as the famous Ruby
on Rails
file { “/etc/hosts”:
content => template(“127.0.0.1 $hostname”)
...
}
... might produce:127.0.0.1 erwin
![Page 26: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/26.jpg)
Facter
● Where all of those cool variables you use in templates come from
● Default fact library includes OS type, version, arch, IP...
$ facter
kernel => Linux
kernelrelease => 2.6.22.991.fc7
lsbdistcodename => Moonshine
lsbdistdescription => Fedora release 7 (Moonshine)
...
![Page 27: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/27.jpg)
Using Facts
● Custom facts can be added to systems● Conditional behavior based on facts
service { ntpd:
...
enable => $operatingsystem ? {
fedora => true,
default => false
}
}
![Page 28: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/28.jpg)
Central Facts Repository
● Server can store client facts in SQL database● Great for running custom reports
+++| name | value |+++| londo.wpi.edu | 71KHT71 || noc1.wpi.edu | F3K8M51 || avocent.wpi.edu | 3KTD351 || delenn.wpi.edu | JDYBSC1 || gkar.wpi.edu | DV6KT71 |+++
![Page 29: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/29.jpg)
Defines
●Create simple functions that can be treated like classes●Can instantiate multiple type definitionsdefine svn_repo($path) {
file { “$path”:
ensure => directory
}
exec { "/usr/bin/svnadmin create $path/$title":
unless => "/bin/test d $path",
require => File[$path]
}
}
svn_repo { puppet: path => "/var/svn" }
![Page 30: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/30.jpg)
Writing Custom Types
● Programmed in Ruby● Simple ones are simple, harder ones are possible● Types are split into two components
– Type: defines type interface– Provider: a specific implementation of backend
● Types can have multiple providers– package: apt, yum, emerge...– user: useradd, pw, netinfo
● Puppet fileserver can be used to push new code out to clients
![Page 31: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/31.jpg)
Simple Sysctl Type
● /etc/sysctl.conf is a simple “key = val” format file containing multiple kernel parameter tweaks
● Example line: net.ipv4.ip_forward = 0
● Sample code only edits file● Uses ParsedFile helper library
![Page 32: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/32.jpg)
Type Code
module Puppet
newtype(:sysctl) do
ensurable
newparam(:name, :namevar => true) do
end
newproperty(:val) do
end
newproperty(:target) do
end
end
end
![Page 33: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/33.jpg)
Provider Code
require 'puppet/provider/parsedfile'
conffile = "/etc/sysctl.conf"
Puppet::Type.type(:sysctl).provide(:parsed,
:parent => Puppet::Provider::ParsedFile,
:default_target => conffile,
:filetype => :flat) do
text_line :comment, :match => /^#/;
text_line :blank, :match => /^\s*$/;
record_line :parsed, :fields => %w{name val},
:joiner => ' = ', :separator => /\s*=\s*/;
end
![Page 34: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/34.jpg)
Sysctl Usage
sysctl { “net.ipv4.ip_local_port_range”:
val = “50000 65535”
}
sysctl { “net.ipv4.ip_forward”:
val = “0”
}
![Page 35: Puppet - WLUG · Writing Custom Types Programmed in Ruby Simple ones are simple, harder ones are possible Types are split into two components – Type: defines type interface –](https://reader033.vdocuments.us/reader033/viewer/2022060313/5f0b6a7e7e708231d430691b/html5/thumbnails/35.jpg)
Conclusions
● We're all stuck on the hamster wheel● Makes easy stuff easy, hard stuff possible● Similar projects
– cfengine– bcfg2
● Additional Resources– http://reductivelabs.com/trac/puppet– http://reductivelabs.com/trac/puppet/wiki/LanguageTutorial– http://reductivelabs.com/trac/puppet/wiki/CompleteConfiguration– #puppet on irc.freenode.org