Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a template for configuration file in Puppet

I am new to Puppet and I am writing a module to setup configuration files. The problem is when multiple clients will be using my module they will have to edit it according to their system. I have heard that templates are way to solve this problem. But I am not able to get it how to use a template for setting up configuration file.

If anyone of you can give me a simple to follow example using templates to configure files would be really helpful. For example how can i setup Apache sites-available default configuration file using template, or give any other example you feel will help a new puppet user. BTW I am on Ubuntu machine.

like image 541
maths Avatar asked Apr 08 '14 21:04

maths


People also ask

Where is Puppet config file?

The puppet. conf file is always located at $confdir/puppet. conf . Although its location is configurable with the config setting, it can be set only on the command line.

How are templates developed in Puppet?

Templates are written in a specialized templating language that generates text from data. Use templates to manage the content of your Puppet configuration files via the content attribute of the file resource type.

Which of the configuration files are included in the content of major configuration file Puppet?

The main configuration file for Puppet is etc/puppet/puppet. conf. All the configuration files get created in a package-based configuration of Puppet.


1 Answers

The PuppetLabs docs on Using Puppet Templates has an example of an Apache configuration for a Trac site. This should be enough to get you started.

Per OP's request, here's a simple example. I'm using NTP rather than the Apache default config since that's a fairly large and complex file. NTP is much simpler.

Directory looks like this:

/etc/puppet/modules/ntp/manifests
                       /templates

Partial contents /etc/puppet/modules/ntp/manifests/init.pp (just the portion defining the template):

$ntp_server_suffix = ".ubuntu.pool.ntp.org"

file { '/etc/ntp.conf':
    content => template('ntp/ntp.conf.erb'),
    owner   => root,
    group   => root,
    mode    => 644,
}

Contents of /etc/puppet/modules/ntp/templates/ntp.conf.erb:

driftfile /var/lib/ntp/drift
<% [1,2].each do |n| -%>
server <%=n-%><%=@ntp_server_suffix%>
<% end -%>

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1

When run with puppet this will result in an /etc/ntp.conf that looks like:

driftfile /var/lib/ntp/drift
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1

This demonstrates a few different concepts:

  1. Variables defined in the puppet manifest (such as $ntp_server_suffix can be accessed as instance variables (@ntp_server_suffix) in the template
  2. Loops and other ruby code can be used in erb templates
  3. Code between <% and %> is executed by ruby
  4. Code between <%= and %> is executed and output by ruby
  5. Code between <%= and -%> is executed and output by ruby and the trailing newline character is suppressed.

Hope this helps you understand templates.

like image 107
Ben Whaley Avatar answered Oct 12 '22 09:10

Ben Whaley