官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 翼城县| 台湾省| 中江县| 齐齐哈尔市| 阜新| 白玉县| 肥乡县| 秦安县| 邓州市| 渭南市| 安丘市| 乐平市| 万荣县| 昔阳县| 左权县| 漳平市| 勃利县| 年辖:市辖区| 临安市| 增城市| 大埔区| 济南市| 聂拉木县| 泾川县| 施甸县| 凌海市| 牡丹江市| 大新县| 邹城市| 昭通市| 望奎县| 英吉沙县| 体育| 澄迈县| 拜城县| 惠水县| 扎赉特旗| 文昌市| 东光县| 屏边| 南平市|