CoursesPuppetIntermediate

Templates (EPP/ERB)

Generate config from data.

Intermediate12 min · lesson 7 of 12

Templates render config files from data, and Puppet has two engines: EPP (Embedded Puppet, the modern choice, using Puppet syntax and typed parameters) and ERB (Embedded Ruby, older). You pass a parameter hash to epp() and reference values in the template; the rendered result becomes a file resource’s content. Same pattern as Ansible/Chef templating — one template plus per-node data replaces many hand-edited files.

templates/nginx.conf.epp
<%- | Integer $workers, Integer $port | -%>
worker_processes <%= $workers %>;
http {
server {
listen <%= $port %>;
server_name <%= $facts['networking']['fqdn'] %>;
}
}
manifest
file { '/etc/nginx/nginx.conf':
ensure => file,
content => epp('nginx/nginx.conf.epp', { 'workers' => 4, 'port' => 8080 }),
notify => Service['nginx'],
}

EPP over ERB

Prefer EPP for new code: it uses the Puppet language you already know (no context-switch to Ruby), declares its parameters with types at the top (so a missing or wrong-typed value fails clearly), and keeps templates consistent with manifests. ERB still appears in older modules and works fine, but EPP’s typed parameter list makes templates safer and self-documenting.

Type the template parameters
An EPP template’s typed parameter block (| Integer $workers, Integer $port |) is not just documentation — it makes Puppet reject a call that omits a value or passes the wrong type, catching the error at compile rather than rendering a broken config. Declare the parameters explicitly instead of reaching into arbitrary scope, so a template’s inputs are obvious and validated.