Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Manifests and Resources

Iteration, Functions, and Lambdas

Loop in Puppet with each, map, filter, reduce, slice and with; declare resources from arrays and hashes, use built-in functions, and understand why Puppet has no for loop.

Puppet has no for or while statement. Instead it has iteration functions that take a lambda (an anonymous block of code). They look like method calls on arrays and hashes, and they let you declare many resources from data. This lesson shows the patterns you will use daily, and the built-in functions that come with them.

Arrays as titles: the simplest loop

When the resources only differ by title, give the type an array:

PUPPET
package { ['git', 'curl', 'tree']:
  ensure => installed,
}

This declares three packages. It is the first thing to try before reaching for a lambda.

each

each runs a lambda for every element. The lambda parameters sit between pipes:

PUPPET
$users = ['alice', 'bob', 'carol']

$users.each |$name| {
  user { $name:
    ensure     => present,
    managehome => true,
  }
}

For a hash, take key and value:

PUPPET
$sites = {
  'blog' => 8081,
  'shop' => 8082,
}

$sites.each |$site, $port| {
  file { "/etc/nginx/conf.d/${site}.conf":
    ensure  => file,
    content => "server { listen ${port}; server_name ${site}.example.test; }\n",
  }
}

For an array you can also get the index: $list.each |$index, $value| { ... }. each is used for its side effect of declaring resources; it returns its input unchanged.

map, filter, reduce

These work on data and return a result:

PUPPET
$doubled = [1, 2, 3].map |$n| { $n * 2 }                 # [2, 4, 6]
$big     = [3, 8, 12, 5].filter |$n| { $n > 4 }          # [8, 12, 5]
$total   = [1, 2, 3, 4].reduce |$sum, $n| { $sum + $n } # 10
$names   = {'a' => 1, 'b' => 2}.filter |$k, $v| { $v > 1 }  # {'b' => 2}

More iteration helpers: slice(n) splits into chunks, with runs a lambda once with given arguments (useful to scope temporary variables), any / all check conditions, partition, group_by, and step. They come from the Puppet language itself, and the stdlib module adds many more functions (flatten, unique, join, keys, values, merge, delete, pick...). Many stdlib functions were folded into core in later versions, so check your version's docs.

Variables inside lambdas

Variables set inside a lambda are local to it. You cannot accumulate into an outer variable (assignment is once-only and the scope is separate). Use map, filter or reduce to build a result instead of trying to mutate:

PUPPET
$ports = $sites.map |$site, $port| { $port }   # [8081, 8082]
$list  = join($ports, ',')                     # '8081,8082'

Functions

Puppet functions fall into two families:

  • Statement functions do something and return nothing: include, require, contain, fail, notice, warning, info, debug, err, tag, realize.
  • Rvalue (returning) functions return a value: lookup, join, split, sprintf, length, upcase, downcase, regsubst, sha256, file, template, epp, dig, merge, empty.

Two ways to call them: prefix style join($ports, ',') and method style $ports.join(','). The latter reads left to right and chains well:

PUPPET
$clean = 'Hello, World'.downcase.regsubst(/[^a-z]/, '', 'G')   # 'helloworld'

notice() and its siblings write to the server log at different levels. notice("port is ${port}") is a classic print debugging tool during compile.

PUPPET
$count = $facts['processors']['count']
$workers = max($count, 2)          # stdlib/core: at least two workers
$label = sprintf('%s-%03d', 'node', 7)   # 'node-007'

You can write your own functions in the Puppet language (inside a module's functions/ folder) or in Ruby (lib/puppet/functions/). A Puppet-language function:

PUPPET
# modules/mymod/functions/port_for.pp
function mymod::port_for(String $site) >> Integer {
  $site.length + 8000
}

Call it as mymod::port_for('shop'). Namespaced names are required for module functions.

Declaring resources from data (a typical pattern)

PUPPET
$vhosts = lookup('web::vhosts', Hash, 'hash', {})

$vhosts.each |$name, $attrs| {
  nginx::vhost { $name:
    port => $attrs['port'],
    root => $attrs['root'],
  }
}

The data comes from Hiera (Hiera), so adding a new site is a data change, not a code change. For this pattern to be cleaner you can use a defined type (Defined Types) and the built-in function create_resources, though each with a lambda is the modern style.

Stand-ins: map, filter, reduce, and data-driven resources

Python Stand-in: each, map, filter, reduce

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: data-driven nginx vhosts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Puppet construct plays the role of a for loop?

    Puppet has no for/while; iteration functions like each take a lambda.
  2. 2. What does [1,2,3].map |$n| { $n * 2 } return?

    map returns a new array with each element transformed.
  3. 3. Why can't you add to an outer variable inside a lambda?

    Use map, filter or reduce to build a new value.
  4. 4. How do you declare three packages with the least code?

    package { ['a','b','c']: ensure => installed } declares all three.

Summary

  • No for loops: use array titles, or each, map, filter, reduce with lambdas.
  • Lambda variables are local; build results with returning iterators.
  • Functions are statement-style (include, fail, notice) or returning (join, lookup, sprintf), with prefix or method call syntax.
  • Declare resources from Hiera data with each for add-a-site-by-editing-data workflows.

Next: Classes and Parameters