Basic Puppet
- Puppet - Facter & Facts
- Puppet - File Server
- Puppet - Module
- Puppet - Manifest Files
- Puppet - Coding Style
- Puppet - Validating Setup
- Installing & Configuring r10K
- Puppet - SSL Sign Certificate Setup
- Puppet - Agent Setup
- Puppet - Master
- Puppet - Environment Conf
- Puppet - Configuration
- Puppet - Installation
- Puppet - Architecture
- Puppet - Overview
Advanced Puppet
- Puppet - Live Project
- Puppet - RESTful API
- Puppet - Type & Provider
- Puppet - Environment
- Puppet - Custom Functions
- Puppet - Function
- Puppet - Classes
- Puppet - Template
- Puppet - Resource Abstraction Layer
- Puppet - Resource
Puppet Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pbrary, which can be used on other projects apart from Ruby pke 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 cpent 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 cpent. There’s no difference on how the cpent sees between using a template and specifying all the content of a file as a string. This clearly indicates that cpent-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] } sympnk { "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 pke this to authenticate users <Location "/cgi-bin/<%= name %>.cgi/login"> AuthType Basic AuthName "Test" AuthUserFile /etc/tomcat/auth/svn Require vapd-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 pke 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 filpng 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 exppcitly 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. #Vapd 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 -%>Advertisements