Skip to content

Functions

OpenVox has a number of built-in functions for loading external data, iteration, logging, manipulating data, and more. This chapter covers the most important ones; the full list is in the built-in function reference.

Template and file functions

epp

The epp function loads and evaluates an EPP template from a module's templates directory:

epp('module_name/filename', { parameter => value, })

epp

inline_epp

The inline_epp function evaluates a string containing an EPP template:

inline_epp('template string', { parameter => value, })

inline_epp

template

The template function loads and evaluates an ERB template from a module's templates directory:

template('module_name/filename')

template

inline_template

The inline_template function evaluates a string containing an ERB template:

inline_template('template string')

inline_template

file

The file function loads a file from a module's files directory:

file('module_name/filename')

file

Logging and failure

Logging

OpenVox supports a number of functions for logging on the server evaluating Puppet code. Each takes a string argument that is logged at an escalating log level:

fail

The fail function causes an immediate catalog compilation failure. Any parameters passed to the function appear in the resulting stack trace.

fail

Iteration

each

each is the most common native iteration function. Given an Iterable data type (such as Array or Hash), each loops over each value and runs a lambda against it:

$array.each |$value| { # Code block }
$hash.each |$key, $value| { # Code block }

each · Iterative functions · Lambdas

create_resources

In older versions of Puppet (pre-Puppet 4) that lacked native iteration, the create_resources function was used to create resources from a hash:

$pkg = {
  'vim' => {
    'ensure' => 'installed',
  },
  'emacs' => {
    'ensure' => 'absent',
  },
}

create_resources('package', $pkg)

With native iteration, the following replaces the create_resources call:

$pkg.each |$key, $value| {
  package { $key:
    * => $value,
  }
}

Flow control

OpenVox supports the following functions for exiting a loop:

Functions that take a lambda

Declaring classes

  • include adds a class and its resources to a catalog.
  • require declares a class, but also adds a dependency on the required class.
  • contain declares a class, including all of its resources as if they were defined in the current class.

Warning

include is the only completely safe way to declare a class from multiple different classes.

Another way to declare a class is with a resource-like class declaration, which is useful for explicitly specifying class parameters as resource attributes:

class { 'classname':
  parameter => 'value',
}

Like any normal resource, duplicate class declarations cause a catalog compilation failure.

Declaring classes

defined

defined determines whether a class, resource type, or variable is available, or whether a resource is declared in a catalog:

defined('typename')
defined('classname')
defined('$variable')
defined(Resource['name'])

defined

Other functions

There are many more built-in functions. A selection:

annotate empty hocon_data new step
assert_type eyaml_lookup_key import partition strftime
binary_file find_file index realize strip
call flatten join regsubst tag
camelcase floor json_data round tagged
capitalize fqdn_rand keys rstrip then
ceiling generate length scanf tree_each
chomp get lest sha1 type
chop getvar lstrip sha256 unique
compare group_by match shellquote unwrap
convert_to hiera max size upcase
dig hiera_array md5 slice values
digest hiera_hash min split versioncmp
downcase hiera_include module_directory sprintf yaml_data