Inside of a Puppet template, you would think that one of these would work to set a default value for something not explicitly defined in your manifest:
my_var ||= 'some default value' my_var = 'some default value' if my_var.nil?
It doesn’t. This will clobber the variable, every time.
To understand why this happens, you need to consider two critical pieces of information:
- The architecture of Puppet is stupidly complicated, which leads to unexpected behaviors all over the place.
- Because Puppet variables are lazy-loaded, they need to reinvent how variables are accessed in the ERb templating system. The Puppet developers, in their infinite wisdom, decided to do this by using method_missing and dumping leaky abstractions all over the place.
To summarize, the reason this doesn’t work is because there really isn’t a variable named my_var at all. ERb tries to find a symbol called my_var and can’t, because that thing that looks like a variable is really syntactic sugar over something completely different happening under the covers.
The correct way to do this is to force a lookup through the scope object as follows:
my_var = scope.lookupvar('my_var') || 'some default value'
The more vested I get in Puppet, the more I want to try out Chef.