Calling Puppet functions from ERB templates

You can use Puppet functions inside ERB templates by calling the scope.call_function(<NAME>, <ARGS>) method.

This method takes two arguments:

  • The name of the function, as a string.

  • All arguments to the function, as an array. This must be an array even for one argument or zero arguments.

For example, to evaluate one template inside another:

<%= scope.call_function('template', ["my_module/template2.erb"]) %>
To log a warning using the Puppet logging system, so that the warning appears in reports:
<%= scope.call_function('warning', ["Template was missing some data; this config file might be malformed."]) %>
scope.call_function was added in Puppet 4.2.

Previous versions of Puppet created a function_<NAME> method on the scope object for each function. These could be called with an arguments array, such as <%= scope.function_template(["my_module/template2.erb"]) %>.

While this method still works in Puppet 4.2 and later versions, the auto-generated methods don’t support the modern function APIs, which are now used by the majority of built-in functions.