Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 16 down vote accepted

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") %> 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.

share|improve this answer
Great, thanks. Word of caution to anyone using the template() function in a template: you still have to qualify it like you do in a puppet manifest (ie, scope.function_template("module/template.erb")). – sh-beta Jul 2 '09 at 13:49
It is possible to use a variable for the name of the template file to load for the template function call within the template? E.g. <%= scope.function_template("sites/$domain/config.erb") %> – Gary Apr 20 '12 at 19:18
Absolutely. Just be mindful that you're working in Ruby, not Puppet's DSL, so the variable names (and interpolation rules) are different. Your string should be <%= scope.function_template("sites/#{domain}/config.erb") %>. – jgoldschrafe Apr 26 '12 at 13:10

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:

module Puppet::Parser::Functions
  newfunction(:namegoeshere, :type => :rvalue) do |args|
    # ...

<%= scope.function_namegoeshere(["one","two"]) %>

Please note it also works without the square braces on some Puppet versions. More info:

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!

<% Puppet::Parser::Functions::function('namegoeshere') %>

More info:

share|improve this answer

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.

share|improve this answer
Native puppet functions are things like fqdn_rand(), sha1(), tagged(), etc. Custom Puppet functions are stored under the modules/modulename/plugins/puppet/parser/functions/. Can these be called from within a template? – sh-beta Jul 1 '09 at 20:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.