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
dev
stage
prod
server.xml.erb
memory = <%= @memory %>
port = <%= @port %>
node
hiera
module
server.xml
Contains text with dynamic ruby code
uses tags to mark dynamic code
<%=
%>
<%
%>
Create a Template
Define Parameters
Update file resource to use template function
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
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
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.
Update the following properties in tomcat.conf in the template, and define the defaults in params.pp
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
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
source => 'puppet:///modules/tomcat/tomcat.conf',
update file: modules/tomcat/manifests/config.pp
content => template('tomcat/tomcat.conf.erb'),
file: modules/tomcat/manifests/config.pp
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']
}
}