Concept of Templating in Puppet

Last updated on May 27 2022
Sarika Tak

Table of Contents

Concept of Templating in Puppet

Puppet – Template

Templating is a method of getting things in a standard format, which can be used in multiple locations. In Puppet, templating and templates are supported using erb which comes as a part of standard Ruby library, which can be used on other projects apart from Ruby like in Ruby on Rails projects. As a standard practice, one needs to have a basic understanding of Ruby. Templating is very helpful when the user is trying to manage content of a template file. Templates plays a key role when configurations cannot be managed by a built-in Puppet type.

Evaluating Templates

Templates are evaluated using simple functions.

$value = template (“testtemplate.erb”)

One can specify the full path of a template or one can pull all templates in Puppet’s templatedir, which is usually located at /var/puppet/templates. One can find the directory location by running the puppet –-configprint templatedir.

Templates are always evaluated by the parser, not the client which means that if one is using puppetmasterd, then the template only needs to be on the server and one never needs to download them to the client. There’s no difference on how the client sees between using a template and specifying all the content of a file as a string. This clearly indicates that client-specific variables are learned first by puppetmasterd during the puppet startup phase.

Using Templates

Following is an example of generating the tomcat configuration for testing sites.

define testingsite($cgidir, $tracdir) {

file { “testing-$name”:

path => “/etc/tomcat/testing/$name.conf”,

owner => superuser,

group => superuser,

mode => 644,

require => File[tomcatconf],

content => template(“testsite.erb”),

notify => Service[tomcat]

}

symlink { “testsym-$name”:

path => “$cgidir/$name.cgi”,

ensure => “/usr/share/test/cgi-bin/test.cgi”

}

}

Following is the template definition.

<Location “/cgi-bin/ <%= name %>.cgi”>

SetEnv TEST_ENV “/export/svn/test/<%= name %>”

</Location>

 

# You need something like this to authenticate users

<Location “/cgi-bin/<%= name %>.cgi/login”>

AuthType Basic

AuthName “Test”

AuthUserFile /etc/tomcat/auth/svn

Require valid-user

</Location>

This pushes each template file into a separate file and then one needs to just tell Apache to load these configuration files.

Include /etc/apache2/trac/[^.#]*

Combining Templates

Two templates can be easily combined using the following command.

template(‘/path/to/template1′,’/path/to/template2’)

Iteration in Templates

Puppet template also supports array iteration. If the variable one is accessing is an array, then one can iterate over it.

$values = [val1, val2, otherval]

We can have templates like the following.

<% values.each do |val| -%>

Some stuff with <%= val %>

<% end -%>

The above command will produce the following result.

Some stuff with val1

Some stuff with val2

Some stuff with otherval

Conditions in Templates

The erb templating supports conditionals. The following construct is a quick and easy way to conditionally put a content in a file.

<% if broadcast != “NONE” %> broadcast <%= broadcast %> <% end %>

Templates and Variables

One can use templates to fill in variables in addition to filling out the file content.

testvariable = template(‘/var/puppet/template/testvar’)

Undefined Variable

If one needs to check if the variable is defined before using it, the following command works.

<% if has_variable?(“myvar”) then %>

myvar has <%= myvar %> value

<% end %>

Out of Scope Variable

One can look for out of scope variable explicitly with the lookupvar function.

<%= scope.lookupvar(‘apache::user’) %>

Sample Project Template

<#Autogenerated by puppet. Do not edit.

[default]

#Default priority (lower value means higher priority)

priority = <%= @priority %>

#Different types of backup. Will be done in the same order as specified here.

#Valid options: rdiff-backup, mysql, command

backups = <% if @backup_rdiff %>rdiff-backup,

<% end %><% if @backup_mysql %>mysql,

<% end %><% if @backup_command %>command<% end %>

<% if @backup_rdiff -%>

 

[rdiff-backup]

 

<% if @rdiff_global_exclude_file -%>

global-exclude-file = <%= @rdiff_global_exclude_file %>

<% end -%>

<% if @rdiff_user -%>

user = <%= @rdiff_user %>

<% end -%>

<% if @rdiff_path -%>

path = <%= @rdiff_path %>

<% end -%>

 

#Optional extra parameters for rdiff-backup

 

extra-parameters = <%= @rdiff_extra_parameters %>

 

#How long backups are going to be kept

keep = <%= @rdiff_keep %>

<% end -%>

<% if @backup_mysql -%>%= scope.lookupvar(‘apache::user’) %>

 

[mysql]

 

#ssh user to connect for running the backup

sshuser =  <%= @mysql_sshuser %>

 

#ssh private key to be used

sshkey = <%= @backup_home %>/<%= @mysql_sshkey %>

<% end -%>

<% if @backup_command -%>

[command]

 

#Run a specific command on the backup server after the backup has finished

 

command = <%= @command_to_execute %>

<% end -%>

 

So, this brings us to the end of blog. This Tecklearn ‘Concept of Templating in Puppet’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Puppet and build a career in DevOps domain, then check out our interactive, Continuous Deployment: Configuration Management using Puppet Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/continuous-deployment-configuration-management-using-puppet/

Continuous Deployment: Configuration Management using Puppet Training

About the Course

Tecklearn has specially designed this Continuous Deployment: Configuration Management using Puppet Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of Configuration Management using Puppet, Puppet Components, important concepts like Puppet Lifecycle, Puppet Language and Puppet Installation. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Puppet.

Why Should you take Configuration Management using Puppet Training?

  • Average salary of Puppet Professional is $90k – Payscale.com
  • Uber, Salesforce, PayPal, Booking.com, MIT, Starbucks. & many other MNC’s worldwide use Puppet across industries.
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Continuous Deployment: Configuration Management using Puppet

  • Need of Configuration Management
  • What is Puppet
  • Puppet Architecture
  • Puppet Components
  • Puppet Lifecycle
  • Setting up Master Slave using Puppet
  • Puppet Manifests
  • Puppet Modules
  • Applying configuration using Puppet
  • Puppet File Server
  • Hands On

 

0 responses on "Concept of Templating in Puppet"

Leave a Message

Your email address will not be published. Required fields are marked *