A collection of task oriented solutions in Puppet

 

Render EPP templates on the command line

Challenge

You want to render EPP templates on the command line

Solution

cat /tmp/epp-template.epp
<%- | String $alias | -%>

ServerName <%= $facts[fqdn] %>
ServerAlias <%= $alias %>
# render the template with this nodes facts
$ puppet epp render /tmp/epp-template.epp --values '{ alias => "notlocal" }'

ServerName puppet01.priv
ServerAlias notlocal

Explanation

Once you start writing Embedded Puppet (EPP) templates you will want to experiment with expanding them with different combinations of values and facts to view the resulting output. Luckily the puppet epp command comes with a flexible subcommand, called render, that will provide most of what you need. Starting with a simple template that takes one parameter (called alias) we'll run it through the render command:

cat /tmp/epp-template.epp
<%- | String $alias | -%>

ServerName <%= $facts[fqdn] %>
ServerAlias <%= $alias %>
# render the template with this nodes facts
$ puppet epp render /tmp/epp-template.epp --values '{ alias => "notlocal" }'

ServerName puppet01.priv
ServerAlias notlocal

In the output we can see both that our specified value is being picked up as a parameter, and used in the output, and that the render command is using the local nodes facts to build the facts hash. If we want to test the template with different fact values we can create a yaml file with a hash of the names to values we want and pass that in to the command too. Here's a simple file with a single override:

$ cat facts.yaml

fqdn: mctesty.priv

We then run the render again with this as an additional command line option.

puppet epp render /tmp/epp-template.epp --values '{ alias => "notlocal" }' --facts facts.yaml
ServerName mctesty.priv
ServerAlias notlocal

While this functionality is great for experimentation and interactive testing once you're happy with how it all works you should move away from this approach and replace the hands on commands with re-usable rspec-puppet tests.

See also