Templates

TOPICS

  • static vs dynamic files

  • templates

  • ERB Syntax

server.dev.xml
server.stg.xml
server.prod.xml
memory = 128mb
port = 8081
memory = 4gb
port = 8888
memory = 16gb
port = 8080

static file

dev

stage

prod

server.xml.erb
memory = <%= @memory %>
port = <%= @port %>

template

node

hiera

module

server.xml

ERB

ERB = EMbedded ruby

Erubis

  • Contains text with dynamic ruby code 
  • uses tags to mark dynamic code

ERB Tags

<%=

%>

<%

%>

group exercise

templatize tomcat

configs

  1. Create a Template
  2. Define Parameters
  3. Update file resource to use template function

steps

we always start with the original configs. Good point to start with s copy over the original config as is and start converting it into a template by replacing parameter values

Create file structure to store the templates

  • Create a director modules/tomcat/templates
  • Create a ERB template  at modules/tomcat/templates/tomcat.conf.erb
  • Copy over the contents of the original tomcat.conf. You could use the link below as a sample content
TOMCAT_CFG_LOADED="1"                                                                                                           
                                                                                                                                
JAVA_HOME="/usr/lib/jvm/jre"
JAVA_OPTS="-Xms64m -Xmx128m -XX:MaxPermSize=128M -Djava.security.egd=file:/dev/./urandom"
                                                                                                                                
CATALINA_BASE="/usr/share/tomcat"                                                                                               
CATALINA_HOME="/usr/share/tomcat"                                                                                               
JASPER_HOME="/usr/share/tomcat"                                                                                                 
CATALINA_TMPDIR="/var/cache/tomcat/temp"                                                                                        
                                                                                                                                
                                                                                                                                
TOMCAT_USER="tomcat"                                                                                                            
                                                                                                                                
SECURITY_MANAGER="false"                                                                                                        
                                                                                                                                
SHUTDOWN_WAIT="30"                                                                                                              
                                                                                                                                
SHUTDOWN_VERBOSE="false"                                                                                                        
                                                                                                                                
CATALINA_PID="/var/run/tomcat.pid"                                                                                              
                                 

file:  modules/tomcat/templates/tomcat.conf.erb

group exercise

Templatize

  • start replacing the values with template variables
  • template variables are enclosed in the marker tags
  • define the sane defaults in tomcat::params class
TOMCAT_USER="tomcat"                                                                                                            
SHUTDOWN_WAIT="30"                                                                                                           
TOMCAT_USER="<%= @user %>"                                                                                                            
SHUTDOWN_WAIT="<%= @shutdown_wait %>"                                                                                         

becomes

  $shutdown_wait = 30

params.pp

TIP : Its always useful to define the params first in params.pp before replacing the values in the template with ERB tags. That way, you would not have to go figure what were the default values, as once replaces, those values are gone. Its also easir to just copy over the params back as template variables.

 

pro tip

exercise

Exercise

Update the following properties in tomcat.conf in the template, and define the defaults in params.pp

  • TOMCAT_CFG_LOADED
  • JAVA_HOME
  • xms
  • xmx
  • maxpermsize
  • CATALINA_BASE
  • JASPER_HOME
  • CATALINA_TMPDIR
  • SECURITY_MANAGER
  • SHUTDOWN_VERBOSE
  • CATALINA_PID
                                                                                                                                
TOMCAT_CFG_LOADED=<%= @tomcat_cfg_loaded %>                                                                                                          
                                                                                                                                
JAVA_HOME=<%= @java_home %>
JAVA_OPTS="-Xms<%= @xms %> -Xmx<%= @xmx %> -XX:MaxPermSize=<%= @maxpermsize %> <%= @java_opts %>"

                                                                                                                                
CATALINA_BASE=<%= @catalina_base %>                                                                                               
CATALINA_HOME=<%= @catalina_home %>                                                                                              
JASPER_HOME=<%= @jasper_home %>                                                                                                
CATALINA_TMPDIR=<%= @catalina_tmpdir %>                                                                                        
                                                                                                                                
                                                                                                                                
TOMCAT_USER=<%= @user %> 

SECURITY_MANAGER=<%= @security_manager %>                                                                                                        
                                                                                                                                
SHUTDOWN_WAIT=<%= @shutdown_wait %> 

SHUTDOWN_VERBOSE=<%= @shutdown_verbose %>                                                                                                        
                                                                                                                                
CATALINA_PID=<%= @catalina_pid %>                                                                                             

file:  modules/tomcat/templates/tomcat.conf.erb

solution

class tomcat::params {

  $color = 'white' 
  $car   = 'figo'
  $user  = 'tomcat'
  $group = 'tomcat'
  $config_path  = '/etc/tomcat/tomcat.conf'
  $packages  = [ 'tomcat', 'tomcat-webapps' ]
  $service_name = 'tomcat'
  $service_state = running
  $tomcat_cfg_loaded = "1"
  $java_home = "/usr/lib/jvm/jre"
  $xms = "64m"
  $xmx = "128m"
  $maxpermsize = "128M"
  $java_opts = " -Djava.security.egd=file:/dev/./urandom"
  $catalina_base = "/usr/share/tomcat"  
  $catalina_home = "/usr/share/tomcat"
  $jasper_home = "/usr/share/tomcat"  
  $catalina_tmpdir = "/var/cache/tomcat/temp" 
  $security_manager = "false"
  $shutdown_wait = "30"
  $shutdown_verbose = "false"
  $catalina_pid = "/var/run/tomcat.pid"
}

tomcat::params class

solution

    source    => 'puppet:///modules/tomcat/tomcat.conf',

update file:  modules/tomcat/manifests/config.pp

to

group exercise

    content  => template('tomcat/tomcat.conf.erb'),

from

file:  modules/tomcat/manifests/config.pp

group exercise

class tomcat::config inherits tomcat{

  file { $::tomcat::config_path:
    #source    => 'puppet:///modules/tomcat/tomcat.conf',
    content  => template('tomcat/tomcat.conf.erb'),
    owner    => $::tomcat::user, 
    group    => $::tomcat::group, 
    mode     => '0644',
    notify   => Service['tomcat'] 
  }

}

APPLY

group exercise

Made with Slides.com