Advanced templating in Puppet

Posted: 2017-07-02 09:54:40

Direct Link | RSS feed

For my web servers I have a puppet class that takes a hostname and sets up all the elements required to create a hosting account including...

  • System user
  • Folder structure, logs folder, public_html etc.
  • Nginx config
  • PHP-FPM config
  • Webalizer config

All the configs are very easily templated except for Nginx. My requirement for this class is that if I have a predefined Nginx config, that puppet should use that, otherwise generate a config from a default template.

This turned out to be a harder task than I anticipated, but I managed to find a solution which could be useful to others.

    $hostname = $title
    $username = $hostname
    $home = "/path/to/hosting/space/$hostname"

    $nginx_config = "/etc/nginx/sites-available/$hostname.conf"

    file { $nginx_config:
        ensure  => 'present',
        owner   => 'root',
        group   => 'root',
        mode    => 0644,
        content => inline_template(
        notify  => Service['nginx'],

It's a bit of a messy solution as we are actually putting a template within the files/ folder. As an alternative you can use an absolute path for file() and put the template within the templates/ folder, but this gets a bit problematic if you change your absolute paths.

It works in the following way....

  • If I have a known Nginx config for this site, I place the file into files/etc/nginx/static_site_files/$hostname.conf.
  • If this file exists it is used and then passed into the inline_template() function. As this file is a straight Nginx config with no Puppet ERB template tags, it is dumped into the file on the Puppet client with no changes.
  • If I haven't got a known config at the path specified and I want to use a default config, it loads the template from files/etc/nginx/default_site_template.conf, then passes it to inline_template which does the correct templating such as log paths, server_name etc.

This means new default sites can be created easily and then customisations are simple to implement.

If you found this useful, please feel free to donate via bitcoin to 1NT2ErDzLDBPB8CDLk6j1qUdT6FmxkMmNz

IT Consultancy Services

I'm now available for IT consultancy and software development services - Cloudee LTD.