Function calls

Functions are plug-ins, written in Ruby, that you can call during catalog compilation. A call to any function is an expression that resolves to a value. Most functions accept one or more values as arguments, and return a resulting value.

 

The Ruby code in the function can do any number of things to produce the final value, including:

  • Evaluate templates.
  • Do mathematical calculations.
  • Look up values from an external source.
  • Cause side effects that modify the catalog.
  • Evaluate a provided block of Puppet code, possibly using the function's arguments to modify that code or control how it runs.
Puppet includes several built-in functions. More functions are available in modules, such as puppetlabs-stdlib, on the https://forge.puppet.com/. You can also write custom functions and put them in your own modules.

An entire function call—including the name, arguments, and lambda—constitutes an expression. It resolves to a single value, and can be used anywhere a value of that type is accepted. A function call might also have an effect, such as adding a class to the catalog. You can also use function calls on their own, which causes their effects to occur while their value is ignored.

All functions run during catalog compilation, which means they can access code and data only from the primary Puppet server. To make changes to an agent node, you must use a resource; to collect data from an agent node, you must use a custom fact.

Each function defines how many arguments it takes, what data types it expects those arguments to be, what values it returns, and any effects it has. For details about any functions built into Puppet, see the function reference. For details about a function included in a module, see that module's documentation.

Statement functions

Statement functions are a group of built-in functions that are used only for their effects, rather than for any values. Puppet recognizes only its built-in statements; it doesn't allow adding new statement functions as plugins. The major difference between statement functions and other functions is that you can omit parentheses when calling a statement function with at least one argument, such as include apache.

Statement functions return a value like any other function, but they always return a value of undef. The built-in statement functions are:

Catalog statements

include: Includes the specified classes in a catalog.
require: Includes the specified classes in the catalog and adds them as a dependency of the current class or defined resource
contain: Includes the specified classes in the catalog and contains them in the current class.
tag: Adds the specified tag or tags to the containing class or defined resource.

Logging statements

debug: Logs message at the debug level.
info: Logs message at the info level.
notice: Logs message at the notice level.
warning: Logs message at the warning level
err: Logs message at the error level.

Failure statements

fail: Logs the error message and terminates compilation.