Advanced templating in Puppet

Posted: 2017-07-02 10:54:40 by Alasdair Keyes

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...

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(
            file(
                "<MODULENAME>/etc/nginx/static_site_files/$hostname.conf",
                "<MODULENAME>/etc/nginx/default_site_template.conf"
            )
        ),
        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....

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

© Alasdair Keyes

IT Consultancy Services

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



Happy user of Digital Ocean (Affiliate link)


Version:master-28fc6e6b4b


Validate HTML 5