- Puppet:Mastering Infrastructure Automation
- Felix Frank Martin Alfke Alessandro Franceschi Jaime Soriano Pastor Thomas Uphillis
- 337字
- 2021-07-09 18:59:08
Leveraging the new template engine
In Chapter 6, Leveraging the Full Toolset of the Language, we introduced templates and the ERB template engine. In Puppet 4, an alternative was added: the EPP template engine. The major differences between the template engines are as follows:
- In ERB templates, you cannot specify a variable in Puppet syntax (
$variable_name
) - ERB templates will not accept parameters
- In EPP templates, you will use the Puppet DSL syntax instead of Ruby syntax
The EPP template engine requires scoped variables from modules:
# motd file – managed by Puppet This system is running on <%= $::operatingsystem %>
The manifest defines the following local variable: <%= $motd::local_variable %>
. The EPP templates also have a unique extension; they can take typed parameters. To make use of this, a template has to start with a parameter declaration block:
<%- | String $local_variable, Array $local_array | -%>
These parameters are not like variables from Puppet manifests. Instead, one must pass parameters using the epp
function:
epp('template/test.epp', {'local_variable' => 'value', 'local_array' => ['value1', 'value2'] })
A template without parameters should only be used when the template is used exclusively by one module, so that it is safe to rely on the availability of Puppet variables to customize the content.
Using the EPP template function with parameters is recommended when a template is used from several places. By declaring the parameters at the beginning, it is especially clear what data the template requires.
There is a specific difference between the template engines when iterating over arrays and hashes. The ERB syntax uses Ruby code with unscoped, local variables, whereas the EPP syntax requires specifying Puppet DSL code:
# ERB syntax <% @array.each do |element| -%> <%= element %> <% end -%> # EPP syntax <% $array.each |$element| { -%> <%= $element %> <% } -%>
The inline ERB function was also supplemented with inline EPP. Using the inline EPP, one can specify a small snippet of EPP code to get evaluated:
file {'/etc/motd': ensure => file, content => inline_epp("Welcome to <%= $::fqdn %>\n") }
Prior to Puppet 4, it was inconvenient to pass more than a small code snippet. With Puppet 4 and the HEREDOC support, complex templates in combination with inline_epp
are easier and better readable.
- Visual C++串口通信開(kāi)發(fā)入門與編程實(shí)踐
- Raspberry Pi for Secret Agents(Third Edition)
- 微服務(wù)設(shè)計(jì)原理與架構(gòu)
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- Java EE 7 Development with NetBeans 8
- Mastering Ext JS
- Hands-On Functional Programming with TypeScript
- Flutter跨平臺(tái)開(kāi)發(fā)入門與實(shí)戰(zhàn)
- Mastering Git
- 智能搜索和推薦系統(tǒng):原理、算法與應(yīng)用
- HTML5開(kāi)發(fā)精要與實(shí)例詳解
- Python大學(xué)實(shí)用教程
- Delphi開(kāi)發(fā)典型模塊大全(修訂版)
- 深入大型數(shù)據(jù)集:并行與分布化Python代碼
- WCF編程(第2版)