Viele User kennen Puppet wahrscheinlich schon aus Ihrem täglichen Arbeitsleben haben aber wahrscheinlich nicht die Zeit es bis auf seine Grenzen auszutesten was dessen Funktionalität betrifft. Dieser Vortrag bietet einen Einblick über Features für fortgeschrittene Anwender mit folgenden Themen, welche durch praktische Beispiele ergänzt werden:
Unterstützung vom neuen Ruby DSL zum Schreiben von Manifesten im reinen Ruby
Die extlookup Funktion zum Speichern von Informationen in externen Formaten
Benutzung von dotty output zur Analyse von Ressourcen - Abhängigkeiten
Exportierte Ressourcen-Abhängigkeiten als Zugang zu generierten Informationen anderer nodes
Externe node classifiers um Puppet in ein externes Tool wie CMDB zu integrieren
Puppetdoc - Generierung und Handhabung zur Dokumentation von Puppet-Inhalten
5. Standalone Puppet
• The Puppet Domain Specific Language (DSL)
is a language that can be used standalone
• Works like any other script
• Requires no infrastructure
Donnerstag, 14. April 2011
7. Example 1
# chmod +x create_file.pp
# ./create_file.pp
notice: /Stage[main]//File[/tmp/example1.out]/ensure:
defined content as '{md5}8a0a86c394456fa1879929d8e1419f94'
notice: Finished catalog run in 0.08 seconds
# cat /tmp/example1.out
Example 1
#
Donnerstag, 14. April 2011
9. Example 2
# chmod +x setup_mysql.pp
# ./setup_mysql.pp
notice: /Stage[main]//Package[mysql-server]/ensure: ensure
changed 'purged' to 'present'
notice: /Stage[main]//File[/etc/mysql/my.cnf]/ensure:
defined content as '{md5}b82b1ec5fa04a2680403e94e5cda845e'
notice: /Stage[main]//Service[mysql]: Triggered 'refresh'
from 1 events
notice: Finished catalog run in 60.37 seconds
#
Donnerstag, 14. April 2011
10. Example 3
# puppet apply -e "file{'/tmp/foo':content=>"foon"}"
notice: /Stage[main]//File[/tmp/foo]/ensure: defined
content as '{md5}d3b07384d113edec49eaa6238ad5ff00'
notice: Finished catalog run in 0.08 seconds
# cat /tmp/foo
foo
#
Donnerstag, 14. April 2011
11. Use Cases
• Boot strapping Puppet on a machine that has
minimal facilities
• Development work
• Running Puppet in master-less mode
Donnerstag, 14. April 2011
13. Why Puppet DSL?
• It’s declarative so its ideal for configuration
management
• We can constrain the behaviour to only
configuration management
• You don’t need to know ruby to start, and its
simpler to learn then most languages
Donnerstag, 14. April 2011
14. Why Ruby DSL?
• Allows you to extend Puppet beyond normal
Puppet DSL
• No constraints
• Prototype new ideas that can potentially
become Puppet DSL later
Donnerstag, 14. April 2011
15. How to use Ruby
DSL
• Puppet now recognises the ‘.rb’ extension
• standalone_script.pp -> standalone_script.rb
• manifests/site.pp -> manifests/site.rb
• <module>/manifests/init.pp -> <module>/
manifests/init.rb
• import “extras.pp” -> import “extras.rb”
Donnerstag, 14. April 2011
16. How to use the
Ruby DSL
#!/usr/bin/env puppet
hostclass :foo do
notice ["foo"]
end
node "default" do
include “foo”
end
standalone_script.rb:
Donnerstag, 14. April 2011
17. Ruby DSL - Nodes
# Puppet DSL
node "default" {
notice(“Default node”)
}
# Ruby DSL
node "default" do
notice[“Default node”]
end
Donnerstag, 14. April 2011
18. Ruby DSL - Classes
# Puppet DSL
class foo {
notice("foo")
}
node "default" {
include foo
}
# Ruby DSL
hostclass :foo do
notice ["foo"]
end
node "default" do
include “foo”
end
Donnerstag, 14. April 2011
19. Ruby DSL - Functions
# Puppet DSL
node "default" {
notice("Function output")
}
# Ruby DSL
node "default" do
notice ["Function output"]
end
# Ruby DSL - alternative
node "default" do
call_function “notice”, ["Function output"]
end
Donnerstag, 14. April 2011
20. Ruby DSL - Resources
# Puppet DSL
define myresource() {
notice($name)
}
node "default" {
myresource {"resource_output": }
}
# Ruby DSL
define "myresource" do
notice [@name]
end
node "default" do
create_resource :myresource, "resource_output"
end
Donnerstag, 14. April 2011
21. Ruby DSL - Variables
# Puppet DSL
node "default" {
$myvar = "value is here"
notice($myvar)
}
# Ruby DSL
node "default" do
scope.setvar("myvar", "value is here")
a = scope.lookupvar("myvar")
notice [a]
end
Donnerstag, 14. April 2011
22. Mysql Example
require 'mysql'
hostclass :packages do
# Connect to mysql
conn = Mysql.new('localhost', 'user', 'password', 'cmdb')
# Obtain a list of mysql packages (and versions)
pkgs = conn.query('select * from packages')
# Create a package resource for each package defining the version
# for each
pkgs.each_hash { |p| package p['name'], :ensure => p['version'] }
conn.close
end
node 'default' do
include 'packages'
end
Donnerstag, 14. April 2011
23. Ruby DSL - Further
Information
• Wiki Article:
• http://projects.puppetlabs.com/projects/1/wiki/
Ruby_Dsl
• Google ‘puppet ruby dsl’
Donnerstag, 14. April 2011
25. Config vs Code
• Node Classifiers
• External Node Classifiers
• Large lookup tables in Puppet DSL
• External data like CSV tables
Donnerstag, 14. April 2011
26. Usage
# Configure extlookup data directory and precedence
$extlookup_datadir = "."
$extlookup_precedence = [
"%{fqdn}",
"location_%{location}",
"county_%{country}",
"common"
]
# Fetch extlookup and print the results
notice(extlookup("snmp_trap_host"))
Donnerstag, 14. April 2011
28. Example - country
snmp_trap_host,1.1.1.1
common.csv:
# ./example1.pp
notice: Scope(Class[main]): 1.1.2.1
notice: Finished catalog run in 0.01 seconds
#
snmp_trap_host,1.1.2.1
country_de.csv:
Donnerstag, 14. April 2011
29. Example - location
snmp_trap_host,1.1.1.1
common.csv:
# ./example1.pp
notice: Scope(Class[main]): 1.1.3.1
notice: Finished catalog run in 0.01 seconds
#
snmp_trap_host,1.1.2.1
country_de.csv:
snmp_trap_host,1.1.3.1
location_holidayinn.csv:
Donnerstag, 14. April 2011
31. dotty output
• Puppet defines every artifact as a resources
• Resources are related to each other through
dependency
• Internally, these dependencies are mapped as
a graph
• These graphs can be output in dotty format
Donnerstag, 14. April 2011
32. dotty: Tools
• Graphviz - open source, very flexible,
widely used
• Gephi - free, java based, powerful graph
support
• Omnigraffle - commercial, more then just
graphs
Donnerstag, 14. April 2011
33. dotty output
Traditional Way:
With new Puppet Interfaces:
puppet --noop --graph --graphdir /vagrant/graphs relationships.pp
puppet catalog --format dot find localhost > ~/Desktop/catalog.dot
Donnerstag, 14. April 2011
44. Nodes - normally
node “app1” {
$snmp_trap_host = “traps.puppetlabs.com”
$dns_server = “1.1.1.1”
include tomcat
include myapp
include resolver
include snmpd
}
node “db1” {
$snmp_trap_host = “traps.puppetlabs.com”
$dns_server = “1.1.1.1”
$mysql_dbs = [“db1”]
include mysql
include resolver
include snmpd
}
Donnerstag, 14. April 2011
45. Why ENC?
• Allows you to store node definitions outside of
Puppet
• Provides Configuration / Code separation
• You can provide information from another
information store such as a CMDB or asset
database
Donnerstag, 14. April 2011
47. API
• First argument passed to the external script is the
hostname
• The script must return valid YAML on standard out
• The script returns 0 for success or non-zero to
indicate the node is ‘not found’
Donnerstag, 14. April 2011
48. YAML Returnednode “app1” {
$snmp_trap_host = “traps.puppetlabs.com”
$dns_server = “1.1.1.1”
include tomcat
include myapp
include resolver
include snmpd
}
---
environment: production
parameters:
snmp_trap_host: traps.puppetlabs.com
dns_server: 1.1.1.1
classes:
- tomcat
- myapp
- resolver
- snmpd
Text
Puppet:
YAML:
Donnerstag, 14. April 2011
51. Puppetdocs
• Works in a similar way to ruby docs or
Perl docs
• Documentation should be close to code
• Can be setup to auto-generate code
from VCS using CI tools like Jenkins
• Supports markdown format
Donnerstag, 14. April 2011