-
Notifications
You must be signed in to change notification settings - Fork 0
Puppet 4 :: Working with Files and Templates
Sandesh Kota edited this page Jul 5, 2018
·
16 revisions
- Andrew Mallet
- ERB: Embedded Ruby Language (any version of puppet). Uses ruby code within tags and require basic ruby knowledge. Variables begin with @ symbol.
- template = read a template
- inline_template = read a string
$down_time = '23:00'
$motd = @(END)
Welcome to the server: <%= @facts['networking']['fqdn'] %>
The server will be shutdown today at: <%= @down_time %>
END
file { 'etc/motd'
ensure => 'file',
content => inline_template($motd),
}
- EPP: Embedded Puppet (Requires Version 4 +). Uses puppet expressions within tags and easier validation tools.
- epp = read a template
- inline_epp = read a string
$motd = @(END)
Welcome to the server: <%= $facts['networking']['fqdn'] %>
The server will be shutdown today at: <%= $down_time %>
END
file { 'etc/motd'
ensure => 'file',
content => inline_epp($motd, {'down_time' => '17:00' }, ),
}
- located in the /templates/ directory
- Create ntp.conf.erb file
# Managed by Puppet - do not edit
server <%= @ntp_local_server %> iburst prefer
server <%= @nto_regional_server %>
driftfile /var/lib/ntp/drift
- Setting variables
#ntp/manifests/admin_file.pp
$ntp_regional_server = $ntp_location ? {
'london' => 'uk.pool.ntp.org',
'paris' => 'uk.pool.ntp.org',
'nyc' => 'uk.pool.ntp.org',
default => 'pool.ntp.org',
}
$ntp_local_server = '192.168.0.0'
#usage
file { $title :
content => template('ntp/ntp.conf.erb') # assumes the template directory so no need to mention
...
}
- Admin.pp file
define ntp:admin_file ( $ntp_location = 'london' ) {
$ntp_regional_server = $ntp_location ? {
'london' => 'uk.pool.ntp.org',
'paris' => 'fr.pool.ntp.org',
'nyc' => 'us.pool.ntp.org',
default => 'pool.ntp.org',
}
$ntp_local_server = '192.168.0.0'
file { $title :
content => template('ntp/ntp.conf.erb'), # assumes the template directory so no need to mention
owner => 'root',
group => 'admingroup',
ensure => file,
}
}
# usage
class ntp::config ( $location = 'london' ) {
ntp::admin_file { '/etc/ntp.conf':
ntp_location => $location,
}
}
- Logic with non-printing tags
# if
<% if @monitor == false -%>
disable monitor
<% end -%>
# loop (each)
$ntp_regional_server = $ntp_location ? {
'london' => ['uk.pool.ntp.org', 'ie.pool.ntp.org'],
'paris' => ['fr.pool.ntp.org', 'de.pool.ntp.org'],
'nyc' => ['us.pool.ntp.org', 'ca.pool.ntp.org'],
default => ['uk.ntp.org', 'us.pool.ntp.org'],
}
<% @ntp_regional_server.each do |s| -%>
server <%= s %>
<% end -%>
- Validating ERB templates (mainly on ruby env not on puppet env) - Syntax Check
- we need to use the 'erb' and the ruby command
- In /opt/puppetlabs/puppet/bin path. set PATH variable #global
- ./erb -PxT '-' <template.erb> | ./ruby -c #relative path
<%= ... %> # Expression tags
<% ... -%> # Ruby Code
- Variables
# Managed by Puppet - do not edit
server <%= $ntp_local_server %> iburst prefer
- Logic with non-printing tags
# if
<% unless $monitor == true { -%>
disable monitor
<% } -%>
- Iteration
# The "-" at the end suppresses the line feed. Ensure that no characters are there after -%> (not even an empty space)
<% $ntp_regional_server.each |$server| { -%>
server <%= $server %>
<% } -%>
- Usage
$monitor = false
$ntp_regional_server = $ntp_location ? {
'london' => 'uk.pool.ntp.org',
'paris' => 'fr.pool.ntp.org',
'nyc' => 'us.pool.ntp.org',
default => 'pool.ntp.org',
}
$ntp_local_server = '192.168.0.0'
file { $title :
content => epp('ntp/ntp.conf.erb', {'monitor' => $monitor, 'ntp_local_server' => $ntp_local_server, 'ntp_regional_server' => $ntp_regional_server, } ),
owner => 'root',
group => 'admingroup',
ensure => file,
}
- Validating EPP templates:
puppet epp validate <EPP file> - Validate & Render EPP templates:
puppet epp validate <EPP file> --values '{ m => 3, param => value}'
- Purging Resources
resources { 'host' :
purge => true, # host resources that are not managed by puppet will be removed
noop => true, # no-operation. Not going to do anything. (remove after development?)
}
# usage
class host_entries {
host { 'localhost':
ip => '127.0.0.1',
}
host { $facts['networking']['fqdn']: # this also centos so error. on other system this should work fine.
ip => $facts['networking']['ip'],
}
host { 'centos7': #2.0 error: Duplicate declaration Host[centos]. Fix check before creating (usign unless)
ip => '192.168.56.15',
}
resources { 'host':
purge => true,
}
}
# puppet apply-e 'include host_entries'
- Encapsulating logic in Defined Types
define host_entries::host_entry ( $ip, ) {
unless $facts['netowrking']['fqdn'] == $title {
host { $title :
ip => $ip,
}
}
}
class host_entries {
host { 'localhost':
ip => '127.0.0.1',
}
host { $facts['networking']['fqdn']:
ip => $facts['networking']['ip'],
}
host_entries::host_entry { 'ubuntu':
ip => '192.168.56.16',
}
host_entries:;host_entry { 'centos7':
ip => '192.168.56.15',
}
resources { 'host':
purge => true,
}
}
- any manual entry to etc/hosts file other than puppet will be removed on the next puppet run because of "purge=>true"