As of 0.24.8, can a Puppet template call a Puppet function?
I can get similar functionality by calling the function in my manifest, assigning the return value to a variable, and then using the lookupvar('variable') in the template, but I'd like to cut out the middle man.
Inside templates you have access to a scope object. All of the functions that you can access in the puppet manifests can be accessed via that scope object, although not via the same name. Prepend
"function_"
to the beginning of the function name. For example, here I included one template inside another:<%= scope.function_template("template2.erb") %>
http://reductivelabs.com/trac/puppet/wiki/PuppetTemplating has great documentation about the scope object inside templates. For really deep information about template and functions you can look inside
lib/puppet/parser/templatewrapper.rb
andlib/puppet/parser/functions.rb
.Let me extend the question also to custom functions. Basically, you call custom functions the same way as built-in functions (using function_ prefix), but make sure you pass all params in an array:
Please note it also works without the square braces on some Puppet versions. More info: https://puppet.com/docs/puppet/latest/functions_ruby_overview.html
Another important thing is there is a bug (or feature) in Puppet 2.6 or older where custom functions are not auto-loaded into the template scope. You need to load them manually, otherwise function wont be found!
More info: http://projects.puppetlabs.com/issues/7991
I'm not entirely sure what you mean by a Puppet function, but puppet templates are embedded Ruby, so you can do anything in the templates that you can in Ruby.