Call Puppet function from Puppet template

puppet

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.

Best Answer

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 and lib/puppet/parser/functions.rb.