我是 Puppet 的新手,我正在编写一个模块来设置配置文件。问题是当多个客户将使用我的模块时,他们将不得不根据他们的系统对其进行编辑。我听说模板是解决这个问题的方法。但是我不知道如何使用模板来设置配置文件。
如果你们中的任何人都可以给我一个使用模板配置文件的简单易懂的示例,那将非常有帮助。例如,我如何使用模板设置 Apache 站点可用的默认配置文件,或者给出您认为对新 puppet 用户有帮助的任何其他示例。顺便说一句,我在 Ubuntu 机器上。
最佳答案
Using 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 list 中定义的变量(例如
$ntp_server_suffix
可以作为模板中的实例变量 (@ntp_server_suffix
) 访问 - 可在 erb 模板中使用循环和其他 ruby 代码
<%
之间的代码和%>
由 ruby 执行<%=
之间的代码和%>
由ruby执行并输出<%=
之间的代码和-%>
由 ruby 执行和输出,尾随的换行符被抑制。
希望这有助于您理解模板。
关于linux - 如何在 Puppet 中使用配置文件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22948509/