Templates
Templates are powerful because they have access to all of the Puppet variables that are present when the template is rendered.
- Facts, global variables, and local variables from the current scope are available to a template as Ruby instance variables — instead of Puppet's $ prefix, they have an @ prefix. (e.g. @fqdn, @memoryfree, @operatingsystem, etc.)
- Variables from other scopes can be accessed with the scope.lookupvar method, which takes a long variable name without the $ prefix. (For example, scope.lookupvar('apache::user').)
The ERB Templating Language
Functions
We've seen several functions already, including include, template, fail, and str2bool, so this is as good a time as any to explain what they are.
Puppet has two kinds of functions:
- Functions that return a value
- Functions that do something else, without returning a value
The template and str2bool functions both return values; you can use them anywhere that requires a value, as long as the return value is the right kind. The include and fail functions do something else, without returning a value — declare a class, and stop catalog compilation, respectively.
All functions are run during catalog compilation. This means they run on the puppet master, and don't have access to any files or settings on the agent node.
Functions can take any number of arguments, which are separated by commas and can be surrounded by optional parentheses:
function(argument, argument, argument)
Functions are plugins, so many custom plugins are available in modules.
Complete documentation about functions are available at the functions page of the Puppet reference manual and the list of built-in functions.