In this lab, we’ll create a short manifest containing a class to which we would like to override the content of a variable.

Here is this manifest :

[root@puppetserver manifests]# cat parameterized_class.pp
class easy::parameterized_class(
  $message = 'hello everybody !'
) {
  notify{ "parameterized class example, message = \"${message}\"": }
}

Where “$message” is the parameter to override by a hiera value.

This class is in a module directory :

[root@puppetserver manifests]# pwd
/examples/standalone_manifests/easy/manifests
[root@puppetserver manifests]# puppet module list | grep /examples/standalone_manifests
/examples/standalone_manifests

Without hiera

Execute this class without hiera :

[root@puppetserver manifests]# puppet apply -e 'class { "easy::parameterized_class": }'
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.36 seconds
Notice: parameterized class example, message = "hello everybody !"
Notice: /Stage[main]/Easy::Parameterized_class/Notify[parameterized class example, message = "hello everybody !"]/message: defined 'message' as 'parameterized class example, message = "hello everybody !"'
Notice: Applied catalog in 0.12 seconds

Using hiera

We already have this class to execute classes defined in hiera :

[root@puppetserver manifests]# cat $(puppet config print manifest)/use_hiera.pp
# tell Puppet server to use hiera :
node default {
  hiera_include('classes')
}

Here is our hiera configuration :

[root@puppetserver manifests]# cat $(puppet config print hiera_config)
---
:backends:
  - yaml
:hierarchy:
  - "nodes/%{::trusted.certname}"
  - common

:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%\PuppetLabs\code\environments\%{environment}\hieradata on Windows
# When specifying a datadir, make sure the directory exists.
  :datadir:

For this node :

[root@puppetserver manifests]# hostname -f
puppetserver.argonay.wou

We define this hiera file :

[root@puppetserver manifests]# cat /etc/puppetlabs/code/environments/production/hieradata/nodes/puppetserver.argonay.wou.yaml
# classes to execute :
classes :
  - easy::parameterized_class

# hiera values :
easy::parameterized_class::message: 'Bybye ...'

Execute our class now :

[root@puppetserver manifests]# puppet apply -e 'class { "easy::parameterized_class": }'
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.36 seconds
Notice: parameterized class example, message = "Bybye ..."
Notice: /Stage[main]/Easy::Parameterized_class/Notify[parameterized class example, message = "Bybye ..."]/message: defined 'message' as 'parameterized class example, message = "Bybye ..."'
Notice: Applied catalog in 0.14 seconds

Ok : “$message” has been overriden by value defined in hiera

Ask Puppet server to execute the class :

[root@puppetserver manifests]# puppet agent -tv
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for puppetserver.argonay.wou
Info: Applying configuration version '1480762312'
Notice: parameterized class example, message = "Bybye ..."
Notice: /Stage[main]/Easy::Parameterized_class/Notify[parameterized class example, message = "Bybye ..."]/message: defined 'message' as 'parameterized class example, message = "Bybye ..."'
Notice: we are executing "easy::my_manifest" class
Notice: /Stage[main]/Easy::My_manifest/Notify[we are executing "easy::my_manifest" class]/message: defined 'message' as 'we are executing "easy::my_manifest" class'
Notice: Applied catalog in 0.15 seconds

Excatly the same : “$message” has been overriden by value defined in hiera

 

www.pdf24.org    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *


*