puppet camp paris 2016 data in modules
TRANSCRIPT
Copyright example42 GmbH - 2016
Puppet 4 - Data in ModulesPuppetCamp Paris 2016
Martin Alfke - [email protected]
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Copyright example42 GmbH - 2016
Martin Alfke!Berlin/Germany !CEO example42 GmbH Freelance Puppet Expert Network !Puppet since 2007 !Puppet Trainer, Consultant !Co-Author of “Puppet 4 Essentials”
Copyright example42 GmbH - 2016
Puppet 4 Data in Modules
• Separation of Code and Data
• Data in Modules
• Lookup Priority
• Data in Component Modules
• Data in Environments
Copyright example42 GmbH - 2016
Separation of Code and Data
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Separation of Code and Data
• data in code class my_ntp {!!if $::environment == ‘dev’ {! $ntp_server = [‘pool.ntp.org’]! } else {! if $::facts[‘fqdn’] == ‘ntp1.example42.com’ {!# switch back to ntp1 when issue is solved! $ntp_server = [‘ntp2.example42.com’]! } else {! $ntp_server = [‘127.0.0.1’]! }!}!
Copyright example42 GmbH - 2016
Separation of Code and Data
• explicit lookup
• hiera(‘key’[, ‘default’][, ‘override hierarchy’])
Copyright example42 GmbH - 2016
Separation of Code and Data
• implicit lookup class my_ntp (!!Array $ntp_server,!) {!!# …!}!!contain my_ntp!!# hiera data!my_ntp::ntp_server:! - ‘pool.ntp.org’!
Copyright example42 GmbH - 2016
Separation of Code and Data
• hiera.yaml # version 1!:backends:! - yaml!:yaml:! :datadir: “/etc/puppetlabs/code/environments/%{environment}/hieradata”!:hierarchy:! - “nodes/%{::trusted.certname}”! - “os/%{::facts[‘os’][‘osfamily’]}”! - common!
Copyright example42 GmbH - 2016
Separation of Code and Data
• hieradata os/Debian.yaml apache::pkgname:! - ‘apache2’! - ‘apache2-ssl’!!os/RedHat.yaml apache::pkgname:! - ‘httpd’!!common.yaml apache::purge_configs: true!
Copyright example42 GmbH - 2016
Separation of Code and Data
• hieradata os/FreeBSD.yaml apache::pkgname:! - ‘apache’!
Copyright example42 GmbH - 2016
Separation of Code and Data
• puppet code # apache/manifests/params.pp class apache::params {! case $::operatingsystem {! ‘Debian’: { # … }! ‘RedHat’: { # … }! default: {! fail(‘OS not supported’)! }! }!}!
Copyright example42 GmbH - 2016
Data in Modules
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Lookup Priority
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Lookup Priority• Hiera -> Global Overrides
!
!
!
Copyright example42 GmbH - 2016
Lookup Priority• Hiera -> Global Overrides
!
• Environment Data -> Core Puppet Lookup
!
Copyright example42 GmbH - 2016
Lookup Priority• Hiera -> Global Overrides
!
• Environment Data -> Core Puppet Lookup
!
• Module Data -> Defaults
Copyright example42 GmbH - 2016
Lookup Functions• Explicit lookup: lookup(‘key’)!
!
• CLI lookup: puppet lookup ‘key’
!
• Automatic lookup: ‘<namespace>::<key>’
Copyright example42 GmbH - 2016
Lookup Functions• lookup(‘key’, <Type>, <merge_behavior>, <default>)!
• e.g. lookup(‘ntp_servers’, Array)
• Merge behavior:
• first!
• unique (array merge)!
• hash!
• deep!
Copyright example42 GmbH - 2016
Data Provider Configuration
• Global Environment Provider: puppet.conf
• environment_data_provider = <data provider>!
!
!
!
Copyright example42 GmbH - 2016
Data Provider Configuration
• Global Environment Provider: puppet.conf
• environment_data_provider = <data provider>!
• Environment Provider: environment.conf
• environment_data_provider = <data provider>!
!
Copyright example42 GmbH - 2016
Data Provider Configuration
• Global Environment Provider: puppet.conf
• environment_data_provider = <data provider>!
• Environment Provider: environment.conf
• environment_data_provider = <data provider>!
• Module Provider: metadata.json
• “data_provider”: “<data provider>”
Copyright example42 GmbH - 2016
Data Provider
• none -> standard hiera lookup
!
• hiera -> hiera lookup (hiera v4)
!
• function -> data function lookup
Copyright example42 GmbH - 2016
Data Provider Hiera
• replace hiera, hiera_array, hiera_hash with ‘lookup’
• needs hiera.yaml v4 configuration file
• set data_provider to ‘hiera’ in puppet.conf, environment.conf or metadata.json
• modify global hiera.yaml to use datadir outside environment
Copyright example42 GmbH - 2016
Data Provider Hiera
# /etc/puppetlabs/code/environments/production/hiera.yaml # /etc/puppetlabs/code/environments/production/modules/<module>/hiera.yaml # - - -!version: 4!datadir: hieradata!hierarchy:! - name: “Nodes”! backend: yaml! path: “nodes/%{trusted.certname}”! - name: “OS”! backend: json! path: “os/%{facts.os.family}”! - name: “common”! backend: yaml
Copyright example42 GmbH - 2016
Data Provider Function
• write data function
• Puppet 4 Function
• <module>/functions/<module>/data.pp
• <env>/functions/<env>/data.pp
• set data_provider to function in puppet.conf, environment.conf or metadata.json
Copyright example42 GmbH - 2016
Data Provider Function - Puppet
# ntp/functions/ntp/data.pp function ntp::data() {! $params = {! ‘ntp::ntpservers’ => [‘pool.ntp.org’],! }! $os_params = case $facts[‘os’][‘family’] {! ‘Debian’: {! { ‘ntp::ntpackage’ => ‘ntpd’, }! },! default: {! {}! }! }! $params + $os_params!}
Copyright example42 GmbH - 2016
Data Provider Function
• write data function
• Ruby Function (Puppet 4 function API)
• <module>/lib/puppet/functions/<module>/data.rb
• <env>/lib/puppet/functions/<env>/data.rb
• set data_provider to function in puppet.conf, environment.conf or metadata.json
Copyright example42 GmbH - 2016
Data Provider Function - Ruby
# ntp/lib/puppet/functions/ntp/data.rb Puppet::Functions.create_function(:’ntp::data’) do! def base_data()! { ‘ntp::ntpservers’ => [‘pool.ntp.org’], }! end! def os_data()! case Facter.value(:os)[‘family’]! when ‘Debian’! { ‘ntp::pkgname’ => ‘ntpd’, }! else! {}! end! def data()! self.base_data.merge!(self.os_data)! end!end
Copyright example42 GmbH - 2016
Data in Component Modules
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Data in Component Modules
• add data provider to metadata.json
• provide OS defaults
• remove params.pp / remove inheritance
• allow users to overwrite any data
Copyright example42 GmbH - 2016
Data Provider Function - Ruby
# my_ntp/manifests/init.pp class my_ntp (! $server = $my_ntp::params::server,! $pkgname = $my_ntp::params::pkgname,! $secure = $my_ntp::params::secure,!) inherits my_ntp::params {! # ...!}!
Copyright example42 GmbH - 2016
Data in Environments
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Data in Environments
• old hiera replacement
• add hiera.yaml to environment base path
• overwrite data from modules, roles & profiles
Copyright example42 GmbH - 2016
Summary
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016
Summary# /etc/puppetlabs/code/ ! ! hiera.yaml!! ! hieradata/!! ! environments/production/ ! ! ! ! ! ! environment.conf!! ! ! ! ! ! hiera.yaml!! ! ! ! ! ! hieradata/! modules/my_module/ ! ! ! ! ! ! ! ! ! ! metadata.json!! ! ! ! ! ! ! ! ! ! hiera.yaml!! ! ! ! ! ! ! ! ! ! hieradata/
Copyright example42 GmbH - 2016
Summary# /etc/puppetlabs/code/ ! ! hiera.yaml!! ! hieradata/!! ! environments/production/ ! ! ! ! ! environment.conf!! ! ! ! ! lib/functions/data.pp!! ! ! ! ! lib/puppet/functions/ \!! ! ! ! ! ! ! ! production/data.rb! modules/my_module/ ! ! ! ! ! ! !! ! metadata.json!! ! ! ! ! ! ! ! lib/functions/data.pp!! ! ! ! ! ! ! ! lib/puppet/functions/ \!! ! ! ! ! ! ! ! ! ! ! my_module/data.rb
Copyright example42 GmbH - 2016
Summary - Pro
• Per hierarchy Hiera Data backend possible
• Data Function lookups without need for hiera backend (e.g. Cloud Management API data)
• No more inheritance required
Copyright example42 GmbH - 2016
Summary - Con
• No single Source of Authority?
• Debugging can be complex when iterating over many data providers and hierarchies
Copyright example42 GmbH - 2016
Module Developers
• switch to data in modules
• give users the possibility to provide own data
• allow users to overwrite any data
• allow users to know their data for missing OS support
Copyright example42 GmbH - 2016
Module Users• switch to hieradata in modules
then
• switch to data in environments
• keep data simple and readable
• don’t overcomplicate !
Copyright example42 GmbH - 2016
Module Users• hieradata
common.yaml my_ntp: ‘pool.ntp.org’!apache::default_mods: false!apache::purge_configs: true!mysql::remove_default_accounts: true!mysql::root_password: ‘puppet’!oradb::database::version: ’12.1’!oradb::shout: ‘MISSING DATA’
Copyright example42 GmbH - 2016
References
• http://docs.puppetlabs.com/puppet/4.3/reference/lookup_quick.html
• http://docs.puppetlabs.com/puppet/4.3/reference/lookup_quick_module.html
• http://puppet-on-the-edge.blogspot.de/2015/01/puppet-40-data-in-modules-and.html
Copyright example42 GmbH - 2016
Puppet 4 - Data in modulesPuppetCamp Paris 2016
Martin Alfke - [email protected]
Image: Tatlin - tatlin.net
Copyright example42 GmbH - 2016