[Puppet Users] ERB strangness, or ruby/puppet internals I don''t understand

Clay Caviness
2011-04-15T19:10:24Z

Let''s say I have a very simple template
template.erb:
<% if not has_variable?("foobar") then foobar =
"undefined" end -%>
foobar: <%= foobar %>
class: <%= foobar.class %>

And a basic manifest:
template.pp:
$mytemp = template(''template.erb'')
notice($mytemp)

And then I apply the manifest, with an undefined foobar (no foobar fact):
$ puppet apply ~/template.pp
notice: Scope(Class[main]): foobar: undefined
class: String

notice: Finished catalog run in 0.01 seconds

Looking good. So now I apply the manifest, but with a defined value
for foobar (via the FACTER_FOOBAR environment variable):
$ FACTER_FOOBAR=''foo'' puppet apply ~/template.pp
notice: Scope(Class[main]): foobar:
class: NilClass

notice: Finished catalog run in 0.01 seconds

Er, what? How on earth did foobar go from a String to NilClass? I
can''t fathom how this is expected, or correct...

The reason I was doing this sort of thing is to give a possibly
missing fact a default value. I ended up using a second variable, like
this:
<% foo = has_variable?("foobar") ? foobar.to_s : "false"
-%>