Skip to content

Instantly share code, notes, and snippets.

@JoshAnderson
Created June 1, 2012 05:28
Show Gist options
  • Save JoshAnderson/2849107 to your computer and use it in GitHub Desktop.
Save JoshAnderson/2849107 to your computer and use it in GitHub Desktop.
Using "local" variables in Puppet templates is a Bad Idea
It turns out that using local variables (like "hostname") in templates
instead of instance variables (like "@hostname") is a really bad idea:
# Ruby treats variables like methods, so we used to expose variables
# within scope to the ERB code via method_missing. As per RedMine #1427,
# though, this means that conflicts between methods in our inheritance
# tree (Kernel#fork) and variable names (fork => "yes/no") could arise.
#
# Worse, /new/ conflicts could pop up when a new kernel or object method
# was added to Ruby, causing templates to suddenly fail mysteriously when
# Ruby was upgraded.
#
# To ensure that legacy templates using unqualified names work we retain
# the missing_method definition here until we declare the syntax finally
# dead.
def method_missing(name, *args)
if scope.include?(name.to_s)
return scope[name.to_s, {:file => file,:line => script_line}]
else
# Just throw an error immediately, instead of searching for
# other missingmethod things or whatever.
raise Puppet::ParseError.new("Could not find value for '#{name}'",@file,script_line)
end
end
That's totally crazy, right?
Source: https://github.com/puppetlabs/puppet/blob/master/lib/puppet/parser/templatewrapper.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment