Puppet Templates: EPP and ERB, Files, Heredocs and Generating Configuration

Puppet Training Course 20 min min read Updated: Nov 17, 2026 Intermediate
Puppet Templates: EPP and ERB, Files, Heredocs and Generating Configuration
Intermediate Topic 4 of 4

Three Ways to Provide File Content

MethodAttributeUse when
Static file from a modulesource => 'puppet:///modules/mod/file.conf'The same content on every node
Text in codecontent => "text\n"Very small files
Templatecontent => epp('mod/file.epp', {...})Content depends on data or facts
puppet # Static file: stored in modules/nginx/files/mime.types file { '/etc/nginx/mime.types': ensure => file, source => 'puppet:///modules/nginx/mime.types', } # Small inline content file { '/etc/motd': ensure => file, content => "Managed by Puppet\n", }

Heredocs

A heredoc is a convenient way to write multi-line text in code.

puppet $banner = @(END) =========================================== Authorized use only. Activity is logged. =========================================== | END file { '/etc/issue.net': ensure => file, content => $banner, } # With interpolation, use a double-quoted tag name: @("END") $text = @("END") Host: ${facts['networking']['fqdn']} | END

EPP: Embedded Puppet Templates (Preferred)

EPP templates use the Puppet language inside <% %> tags and take explicit parameters, which makes them easy to read and test. Store them in templates/ of a module with the extension .epp.

text <%# templates/vhost.conf.epp : this is a comment %> <%- | String $server_name, Integer[1,65535] $port = 80, Array[String] $aliases = [], Boolean $ssl = false | -%> server { listen <%= $port %>; server_name <%= $server_name %><% $aliases.each |$a| { %> <%= $a %><% } %>; <% if $ssl { -%> ssl_certificate /etc/ssl/certs/<%= $server_name %>.pem; ssl_certificate_key /etc/ssl/private/<%= $server_name %>.key; <% } -%> location / { proxy_pass http://127.0.0.1:8080; } }
puppet file { '/etc/nginx/conf.d/shop.conf': ensure => file, content => epp('profile/vhost.conf.epp', { 'server_name' => 'shop.example.com', 'port' => 8080, 'aliases' => ['www.shop.example.com'], 'ssl' => true, }), }
EPP tagMeaning
<%= expression %>Insert the value
<% code %>Run code (if, each) without printing
<%# comment %>Comment, not printed
<%- ... -%>A leading - removes spaces before the tag, a trailing - removes the newline after it. Use them to avoid blank lines
<%% %%>Literal tag characters

ERB: The Older Ruby-Based Templates

ERB templates use Ruby inside the tags and read variables from the surrounding class as @variable. You will meet them in older modules.

text # templates/app.conf.erb # Managed by Puppet on <%= @fqdn %> port=<%= @port %> <% @servers.each do |s| -%> server=<%= s %> <% end -%> <% if @debug -%> debug=true <% end -%>
puppet class app (Integer $port = 8080, Array $servers = [], Boolean $debug = false) { file { '/etc/app/app.conf': ensure => file, content => template('app/app.conf.erb'), # uses variables of the class: @port, @servers, @debug } }
EPPERB
LanguagePuppetRuby
VariablesExplicit parameters, $nameImplicit scope, @name
FactsPass in or use $facts[...]@fqdn or scope['facts']
RecommendationUse for new codeKeep for existing code

Inline Templates for Quick Checks

bash puppet apply -e 'notice(inline_epp("<%= $x * 2 %>", { "x" => 21 }))' puppet epp validate templates/vhost.conf.epp # syntax check puppet epp render templates/vhost.conf.epp --values '{ server_name => "demo.local" }' # preview output erb -P -x -T '-' templates/app.conf.erb | ruby -c # check an ERB template

Useful Patterns

Config files with hashes

text <%# templates/sshd_config.epp %> <%- | Hash[String, Variant[String, Integer, Boolean]] $settings | -%> # Managed by Puppet <% $settings.each |$k, $v| { -%> <%= $k %> <%= $v ? { true => 'yes', false => 'no', default => $v } %> <% } -%>
puppet file { '/etc/ssh/sshd_config': ensure => file, mode => '0600', content => epp('ssh/sshd_config.epp', { 'settings' => { 'PermitRootLogin' => false, 'Port' => 22, 'PasswordAuthentication' => false }, }), validate_cmd => '/usr/sbin/sshd -t -f %', # test the file before replacing the real one notify => Service['sshd'], }
validate_cmd: For files that can break a service when wrong (sshd, sudoers, nginx), use validate_cmd. Puppet writes a temporary file, runs your command on it (% is the temp path) and only installs the file if it passes.

Managing Part of a File

When you only need one line, the stdlib module provides file_line and concat helps to assemble a file from fragments. Fully managing a file with a template is usually cleaner.

puppet file_line { 'disable root ssh': path => '/etc/ssh/sshd_config', line => 'PermitRootLogin no', match => '^#?PermitRootLogin', }

Best Practices

  • Add a header # Managed by Puppet. Manual changes will be overwritten to every managed file
  • Keep logic out of templates. Compute values in the class, pass simple data to the template
  • Always give EPP parameters types, with defaults where sensible
  • Validate and render templates in unit tests (later lesson)
  • Use Sensitive data in content for files that contain secrets, so diffs hide it
  • Set permissions explicitly on every file you manage

Practice Tasks

  1. Write an EPP template for an /etc/hosts-style file from an array of hashes
  2. Generate an nginx virtual host from a template and preview it with puppet epp render
  3. Use validate_cmd for a sudoers.d file with visudo -cf %
  4. Convert a small ERB template to EPP
  5. Use file_line to set one value in /etc/ssh/sshd_config and compare with managing the full file

Conclusion

Templates generate configuration from data. Use EPP with typed parameters for new code, keep logic in classes, and protect risky files with validate_cmd and clear headers.

Quick Summary: source for static; EPP typed params; ERB legacy; heredocs; dash tags trim whitespace; validate_cmd; puppet epp render.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators