Class Parameters
- All modules should contain a
params
class.
$ cd ~/helloworld
.
|--manifests
| |-- init.pp
| |-- params.pp
- All default values for the module or internal classes should be declared in the
params
class.
class helloworld::params{
$salutation = 'Hello'
$who = 'World'
}
- All internal classes should only inherit from the
params
class. Any other form of inheritance should be avoided.
class helloworld inherits helloworld::params{
...
}
- The only goal of the
params
class is to provide default values for the rest of the module's class parameters.
# manifests/init.pp
class helloworld(
$salutation = $::helloworld::params::salutation,
$who = $::helloworld::params::who,
) inherits helloworld::params{
file{'/tmp/hello-world.txt':
content => inline_template("<%= @salutation %>, <%= @who %>"),
}
}
- All top level variables like
facters
, hiera
lookups, scope
lookups should terminate in the params
class.
class helloworld::params{
if $::tag_salutation == undef {
$salutation = 'Hello'
notice("tag_salutation not defined using ${salutation}")
} else {
$salutation = $::tag_salutation
}
# Getting values from Hiera
if hiera('who') == undef {
$who = 'World'
notice("who not defined using ${who}")
} else {
$who = hiera('who')
}
}
- Variables should always be
local
. The only allowed namespaced variables should be the ones that the class parameters uses as defaults.
class mymodule(){
include directories
$directory = "${::directories::default_path}/a-directory"
file{$directory:
ensure => directory,
require => File[$::directories::default_path]
}
}