I often want to include default values in Puppet templates. I was hoping that given a class like this:
class myclass ($a_variable=undef) {
file { '/tmp/myfile':
content => template('myclass/myfile.erb'),
}
}
I could make a template like this:
a_variable = <%= a_variable || "a default value" %>
Unfortunately, undef
in Puppet doesn't translate to a Ruby nil
value in the context of the template, so this doesn't actually work. What is the canonical way of handling default values in Puppet templates?
I can set the default value to an empty string and then use the empty?
test...
a variable = <%= a_variable.empty? ? "a default value" : a_variable %>
...but that seems a little clunky.
somehash['key-that-does-not-exist'] || "a value"
and it works just fine). So yes, he sounds a bit frustrated, but he's not completely off base. The syntax is inconsistent and there's not a lot of documentation on how Puppet's variable behave different from Ruby variables.my_var ||= 'some default value
in a manifest (i.e. foo.pp), but instead he is talking about templates and ERB. There is certainly a point to be made about the various inconsistent and non-intuitive ways variables are handled in templates. The wholescope.lookupvar()
business since 2.7.0, for example. But I think in that way Puppet is still evolving, and I don't think Chef has found the answers to all that, like the blog post seems to be implying.