This document discusses setting up a manageable Puppet infrastructure. It outlines common pitfalls like unmaintainable codebases and collaboration difficulties. Quick wins to address pitfalls include moving data to Hiera, implementing code reviews, and refactoring constantly. The document recommends designing around server roles, storing all things data in Hiera, and facilitating deployment and workflow through environments and R10k. It provides examples of setting up roles, using different Hiera data types, and Hiera-related functions to retrieve and generate resources from data.
6. Pitfalls
Cause & effect
Quick Wins
Fix your codebase!
Quick wins:
Move data to Hiera
Implement Code Review
Use Puppet-lint in a git-hook
REFACTOR CONSTANTLY
6 / 44
19. How to do it?
Createroles module
root@puppet# puppet module generate gerapeldoorn-role
Createabase-roleto cover generic settings
# modules/role/manifests/base.pp:
class role::base {
include users
include ssh
include motd
...
19 / 44
20. How to do it? -Cont'd-
Put all requiredresources intheclasses
# modules/role/manifests/app.pp:
class role::app {
include apache
include tomcat
apache::virtualhost { 'default':
...
Includeroleinnodedefinition
# site.pp:
node 'app01.autiplan.com' {
include role::base
include role::app
}
20 / 44
23. ConfiguredHierarchy:
#/etc/puppet/hiera.yaml:
:hierarchy:
- "%{::clientcert}"
- "%{::environment}"
- common
Node app01.autiplan.com:
environment: testing
Hieradata
# hiera/app01.autiplan.com.yaml
---
examplekey: value for
app01.autiplan.com
# hiera/testing.yaml
---
examplekey: value for nodes in
testing environment
# hiera/common.yaml
---
examplekey: value for all nodes
It's all about Hierarchy
What will bein$test?
$test = hiera('examplekey')
23 / 44
29. Whatdoes itdo?
Retrieves the first-found value in the
hierarchy. (top-down)
Whatto use itfor?
Basic variable-lookup.
Very easy to create exceptions!
Howto use it?
$smarthost = hiera('smarthost')
ExampleHieradata
# hiera/mail.autiplan.com.yaml
---
smarthost: smtp.myprovider.nl
# hiera/testing.yaml
---
smarthost: testsmtp.autiplan.com
# hiera/common.yaml
---
smarthost: mail.autiplan.com
hiera('key' [,default_value])
29 / 44
30. Whatdoes itdo?
Retrieves an array or hash value
in the hierarchy, concatinates all
found results
Whatto use itfor?
Combining data from all
hierarchy levels.
Howto use it?
$users = hiera_array('users')
ExampleHieradata
# hiera/app01.autiplan.com.yaml
---
users: [ 'user1', 'user2' ]
# hiera/testing.yaml
---
users: [ 'testuser' ]
# hiera/common.yaml
---
users: [ 'user3', 'user4' ]
hiera_array('key' [,default_value]) (andhiera_hash)
30 / 44
31. Whatdoes itdo?
Includes all classes listed in the
array that is loaded from Hiera.
Takes elements from ALL
hierarchy levels.
Whatto use itfor?
Lightweight ENC.
Put all classes / roles in Hiera.
Howto use it?
node default {
hiera_include('roles')
}
ExampleHieradata
# hiera/web01.autiplan.com.yaml
---
roles:
- role::web
# hiera/common.yaml
---
roles:
- role::base
hiera_include('classes')
31 / 44
32. Whatdoes itdo?
Generates resources from a
HASH.
Whatto use itfor?
Generate any resource based on
data from Hiera.
Can also be used with
hiera_hash to create resources
from all levels!
Howto use it?
create_resources ('apache::vhost', hiera('vhosts', {}))
ExampleHieradata
# hiera/web01.autiplan.com.yaml
---
vhosts:
autiplan.com:
alias: www.autiplan.com
autiplan.dk:
alias: www.autiplan.dk
docroot: /var/www/html/autiplan.dk
autiplan.nl:
alias: www.autiplan.nl
cdn.autiplan.com:
port: 81
docroot: /var/www/html/cdn
create_resources('type',HASH[,default_values])
32 / 44
34. Whatdoes itdo?
Automatically loads class
parameters from Hiera.
Whatto use itfor?
Specify all class parameters in
Hiera.
Use all hierarchical benefits for
class parameters.
Simplify the use of
parameterized classes.
Howto use it?
include mysql::server
ExampleHieradata
# hiera/web01.autiplan.com.yaml
---
mysql::server::root_password: m0ars3cr3t
# hiera/common.yaml
---
mysql::server::root_password: t0ps3cr3t
mysql::server::package_name: mysql-server
mysql::server::restart: true
Data bindings
34 / 44
35. Putting it all together
Anything node-specific should be in Hiera!
35 / 44
39. Environments
What is anenvironment?
Seperate modulepaths/site.pp.
Common environments: development, testing, production.
Nodes request a specific environment.
Why?
Essential to prevent mistakes.
NEVER edit code in production!
The workflow helps us to 'promote' our code to production.
39 / 44
42. Final remarks
Keeppublic modules as-is,wherever possible
Create wrapper classes in company-module.
Create fork if needed, submit pull request for fixes.
Add forked module (gitrepo) to Puppetfile.
Thinkahead
Always try to anticipate future applications.
If it feels overly complicated, yer doin it wrong.
Refactor!
42 / 44