up vote 5 down vote favorite
1
share [g+] share [fb]

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.

link|improve this question

61% accept rate
feedback

2 Answers

up vote 8 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") %>

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.

link|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
feedback

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.

link|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
feedback

Your Answer

 
or
required, but never shown

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