Learning lambdas and functions
Functions have long been an essential part of Puppet. Due to the new type system, a complete new set of functions have become possible functions with different behavior based on parameter data types.
To understand functions, we first have to take a look at lambdas, which are introduced in Puppet 4. Lambdas represent a snippet of Puppet code, which can be used in functions. Syntactically, lambdas consist of an optional type and at least one variable with optional defaults set, enclosed in pipe signs (|
), followed by Puppet code inside a block of curly braces:
$packages = ['htop', 'less', 'vim'] each($packages) |String $package| { package { $package: ensure => latest, } }
Lambdas are typically used on functions. The preceding example uses the each
function on the $packages
variable, iterating over its contents, setting the lambda variable $package
within each iteration to the values htop
, less
, and vim
, respectively. The Puppet code block uses...