What is the proper way to check if a variable is undef in a puppet template?
In the manifest the variable is defined as follows
$myvar = undef
How is this checked in the template?
Is saw the following two variants
<% if @myvar -%> <% end -%>
and
<% if not @myvar.nil? and @myvar -%> <% end -%>
They both seem to work in my case, but I wonder if the first approach fails in on certain cases?
Puppet's undef value is roughly equivalent to nil in Ruby. It represents the absence of a value. If the strict_variables setting isn't enabled, variables which have never been declared have a value of undef . The undef value is useful for testing whether a variable has been set.
Script written in ERB, a templating language for Ruby; may include any type of plain text or source code, but also includes Ruby ERB code that generates additional text into the resulting file when run with the ERB template engine. ERB is often used for templating Web files such as . RB, .
The Puppet documentation (at the time of writing this answer) explains it very well: https://puppet.com/docs/puppet/latest/lang_template_erb.html#concept-5365
Since undef
is not the same as false
, just using an if
is not a good way to check for it. Also when a variable is defined, but has a value of false
or nil
it is also impossible to check with a simple if
.
This is why you want to use scope.lookupvar(‘variable’)
and check its return value for :undef
or :undefined
(or nil
) to know if it was set to undef
, or never set at all.
I'd say the check depends on whether you want an alternative when the variable is not defined.
I'm using the following rules:
Check in your puppet script whether the variable contains the expected value before even considering template rendering:
if $myvar == undef { fail {"You really must set myvar, seriously."} } if ! $anothervar { fail {"anothervar is false, undefined or empty."} }
You can make your life easier by setting the type of parameters explicitly. This spares you type comparisons and conversions.
In your template you simply write the variables then:
<%= @myvar %> <%= @anothervar %>
If you assume the variable is defined, you can treat it as boolean.
The mapping is as follows (source):
In Puppet >=4:
Examples:
print 'something' if @myvar evaluates to true, otherwise 'something else'.
<% if @myvar %>something<% else %>something else<% end %>
print 'something' if @myvar evaluates to true
<% if @myvar %>something<% end %>
print @myvar if it evaluates to true, otherwise 'alternative' %>
<%= @myvar ? @myvar : 'alternative' %>
If you are not sure a variable is even defined and don't want to make wrong assumptions, check it in the template.
Examples:
print 'something' followed by @myvar if @myvar is defined and not empty
<% if defined?(@myvar) && ! @myvar.empty? %>something<%= @myvar %><% end %>
print @myvar if it's defined and greater than 10
<%= @myvar if defined?(@myvar) && @myvar > 10 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With