Puppet templates and undefined/nil variables

Posted by larsks on Server Fault See other posts from Server Fault or by larsks
Published on 2012-04-10T17:14:22Z Indexed on 2012/04/10 17:31 UTC
Read the original article Hit count: 275

Filed under:
|
|

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.

© Server Fault or respective owner

Related posts about puppet

Related posts about ruby