A collection of task oriented solutions in Puppet

 

Select an EPP template based on a fact

Challenge

You want to deploy a specific EPP template based on the value of a fact.

Solution

# install the external multi_epp module
$ sudo /opt/puppetlabs/bin/puppet module install deanwilson-multi_epp

Notice: Installing -- do not interrupt ...
/etc/puppetlabs/code/environments/production/site
|-- deanwilson-multi_epp(v0.1.0)
class ssh::config {

  file { '/etc/ssh/sshd_config':
    ensure  => present,
    mode    => '0600',
    content => multi_epp( [
                            "ssh/${::fqdn}.epp",
                            "ssh/${::domain}.epp",
                            'ssh/default_sshdconfig.epp',
                          ], {
                                'port'          => 22222,
                                'ListenAddress' => '0.0.0.0',
                          }),
  }

}

Explanation

Sometimes you want to have specific versions of an Embedded Puppet (EPP) template for a given host, location or operating system while having a default fallback for the standard cases. Unfortunately while this is easy to do with file resources there is no equivalent in core puppet for EPP templates. Instead you'll need to Install a module from the forge, in this case multi_epp to add this functionality.

# install the external multi_epp module
$ sudo /opt/puppetlabs/bin/puppet module install deanwilson-multi_epp

Notice: Installing -- do not interrupt ...
/etc/puppetlabs/code/environments/production/site
|-- deanwilson-multi_epp(v0.1.0)

Once you've installed the module you replace epp with multi_epp in your resources and you can specify multiple sources as an array, with the value of a fact as part of the on-disk file name, in the same way you would select from multiple normal files.

In the module directory you would have file names like these:

templates/
    tuning.host1.cnf.epp
    tuning.host2.cnf.epp
    tuning.example.org.cnf.epp
    tuning.cnf.epp

When using multiple sources in this way puppet will use the first template file that exists, and cause an error if none match, so you should nearly always have a default file.

See also