Using Puppet Templates 上的 PuppetLabs 文档有一个 Trac 站点的 Apache 配置示例。这应该足以让您入门。
根据 OP 的要求,这是一个简单的示例。我使用的是 NTP 而不是 Apache 默认配置,因为这是一个相当大且复杂的文件。 NTP 要简单得多。
目录如下:
/etc/puppet/modules/ntp/manifests
/templates
部分内容/etc/puppet/modules/ntp/manifests/init.pp(只是定义模板的部分):
$ntp_server_suffix = ".ubuntu.pool.ntp.org"
file { '/etc/ntp.conf':
content => template('ntp/ntp.conf.erb'),
owner => root,
group => root,
mode => 644,
}
/etc/puppet/modules/ntp/templates/ntp.conf.erb的内容:
driftfile /var/lib/ntp/drift
<% [1,2].each do |n| -%>
server <%=n-%><%=@ntp_server_suffix%>
<% end -%>
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
当使用 puppet 运行时,这将导致 /etc/ntp.conf 看起来像:
driftfile /var/lib/ntp/drift
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
这展示了几个不同的概念:
- 在 puppet 清单中定义的变量(例如
$ntp_server_suffix 可以作为模板中的实例变量 (@ntp_server_suffix) 访问
- 循环和其他 ruby 代码可以在 erb 模板中使用
-
<% 和 %> 之间的代码由 ruby 执行
-
<%= 和 %> 之间的代码由 ruby 执行和输出
-
<%= 和 -%> 之间的代码由 ruby 执行和输出,结尾的换行符被抑制。
希望这有助于您理解模板。