Skip to content

Instantly share code, notes, and snippets.

@stefanozanella
Created December 1, 2012 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanozanella/4180151 to your computer and use it in GitHub Desktop.
Save stefanozanella/4180151 to your computer and use it in GitHub Desktop.
Puppet implementation patterns: Safe Default

Safe Default

When

You have a defined type and want to use a default value for a parameter from a params class.

How

Set the default value of the variable as undef, then put a conditional after including the params class in the class body in which you set another internal variable with the value of the params class variable or the user-defined value.

Notes

The most elegant way to do this would be to directly reference the variable as the default value of the parameter, such as in:

  define test ( $myparam = $test::params::myparam ) {
    include test::params
    ...

This, however, won't work unless you include the test::params class in a higher scope.

Drawbacks

Code for the defined type tend to grow quite a bit with the number of parameters. Also, since parameters cannot be reassigned, one is forced to introduce a new local variable; this worsen code readability and introduce a problem of clashing names.

class test::params {
$message = "Default message from params class"
}
# This will work with a parameterized class as well
# but inheritance would do a cleaner job
define test (
$message = undef,
) {
include test::params
$real_message = $message ? {
undef => $test::params::message,
default => $message
}
notify { $real_message: }
}
test { "simple notification": }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment