How to use a template for configuration file in Puppet

maths

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.

Ben Whaley

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use puppet to format a partition with a file system

From Dev

How to change template directory in puppet

From Dev

How to use setm in puppet

From Dev

How to make use of variables in Wildfly configuration file?

From Dev

How to use configuration file for Azure web app

From Dev

How to Use Mail Merge Template file with PHP

From Dev

how to use property file using jdbc template

From Dev

How to use base template file for golang html/template?

From Dev

Puppet inline template with puppet:// in URL

From Dev

passing parameters from external file (template) to puppet script

From Dev

How to use return value from a Puppet exec?

From Dev

Puppet, how to use the exec return value as a trigger?

From Dev

Puppet, how to use the exec return value as a trigger?

From Dev

How to use conditional operation in augeas puppet

From Dev

How should I use quoting with echo in Puppet?

From Dev

How to calculate value in puppet erb file

From Dev

How to copy file to all clients using puppet?

From Dev

How to test for existence of a file on the Puppet Master

From Dev

How to copy file to all clients using puppet?

From Dev

Puppet semodule: how to add a .te file?

From Dev

How to provide a startup service file in Puppet

From Dev

Puppet erb template - how do you compare two integers

From Dev

How to read entire files in Puppet verbatim (without parsing like template)?

From Dev

Puppet and Agent configuration

From Dev

Puppet cluster configuration in AWS

From Dev

How can I use a parameter to a protractor configuration file to chose steps?

From Dev

How do I use puma's configuration file?

From Dev

How to use current file as parameter to Run configuration in pycharm

From Dev

How can i get and use AspnetUsers UserId in migration configuration file?

Related Related

  1. 1

    How to use puppet to format a partition with a file system

  2. 2

    How to change template directory in puppet

  3. 3

    How to use setm in puppet

  4. 4

    How to make use of variables in Wildfly configuration file?

  5. 5

    How to use configuration file for Azure web app

  6. 6

    How to Use Mail Merge Template file with PHP

  7. 7

    how to use property file using jdbc template

  8. 8

    How to use base template file for golang html/template?

  9. 9

    Puppet inline template with puppet:// in URL

  10. 10

    passing parameters from external file (template) to puppet script

  11. 11

    How to use return value from a Puppet exec?

  12. 12

    Puppet, how to use the exec return value as a trigger?

  13. 13

    Puppet, how to use the exec return value as a trigger?

  14. 14

    How to use conditional operation in augeas puppet

  15. 15

    How should I use quoting with echo in Puppet?

  16. 16

    How to calculate value in puppet erb file

  17. 17

    How to copy file to all clients using puppet?

  18. 18

    How to test for existence of a file on the Puppet Master

  19. 19

    How to copy file to all clients using puppet?

  20. 20

    Puppet semodule: how to add a .te file?

  21. 21

    How to provide a startup service file in Puppet

  22. 22

    Puppet erb template - how do you compare two integers

  23. 23

    How to read entire files in Puppet verbatim (without parsing like template)?

  24. 24

    Puppet and Agent configuration

  25. 25

    Puppet cluster configuration in AWS

  26. 26

    How can I use a parameter to a protractor configuration file to chose steps?

  27. 27

    How do I use puma's configuration file?

  28. 28

    How to use current file as parameter to Run configuration in pycharm

  29. 29

    How can i get and use AspnetUsers UserId in migration configuration file?

HotTag

Archive