how to pass parameters to puppet modules?
What is the best practice for configuration of puppet modules? I have puppet 2.7.11
. I find this way quite messy, it looks like using global variables.
node default {
$always_apt_update = true
include apt
}
Should I create class which would inherit most of configuration from the original? The documentation seems to have too many versions and I'm not sure which one applies for me.
UPDATE:
when I try this:
class { 'apt':
always_update => 'true',
}
I get an error:
Error 400 on SERVER: Invalid parameter always_update at /etc/puppet/manifests/nodes.pp:32
Solution 1:
You should use Parametrized classes instead of global variables.
For example:
node default {
class {'apt':
always_update =>true
}
}
class apt ($always_update = true ) {
// code
}
node 'example.com' {
class { bar: }
}
See puppet documentation for more information:
- http://projects.puppetlabs.com/projects/1/wiki/Development_Language_Evolution#Parameterized+classes
- http://docs.puppetlabs.com/guides/language_guide.html#parameterised-classes
Solution 2:
These answers seems a bit outdated, with new versions of puppet i.e 3.7.x, class parameters can be passed using Hiera. Resource like class declaration is not considered best practice anymore.
Although the second answer does use Hiera, but it is using ‘hiera’ function explicitly, which is again a not so advisable practice.
The new solution would look something like this:
/etc/puppet/manifests/site.pp
:
node default {
include apt
}
/etc/puppet/modules/apt/manifests/init.pp
:
class apt ($always_update = true ) {
// code
}
/etc/puppet/hieradata/<filename>.yaml
apt::always_update: true
Hiera data yaml files can have different values of the parameter as required.
Solution 3:
The recommended practice these days is to use Hiera. It's built into 3.x, but in 2.7 you need to install it separately.
You can then grab data from Hiera in your manifest for the apt
class:
$always_apt_update = hiera("always_apt_update")
With a Hiera config like this..
:hierarchy:
- %{::clientcert}
- common
..the node (pulled from the clientcert
fact) will be used as higher precedence in the lookup than the common.yaml
file.
So, with always_apt_update: false
in node1.example.com.yaml
and always_apt_update: true
in common.yaml
, node1 will end up with that variable set to false while other nodes will have it default to true.