我是Puppet的新手,我正在编写一个模块来设置配置文件。问题是当多个客户端将使用我的模块时,他们将不得不根据他们的系统进行编辑。我听说模板是解决这个问题的方法。但我无法得到如何使用模板来设置配置文件。
如果有人能给我一个简单易懂的示例,使用模板配置文件会非常有帮助。例如,如何使用模板设置Apache站点 - 可用的默认配置文件,或者给出您认为有助于新木偶用户的任何其他示例。顺便说一下,我在Ubuntu机器上。
答案 0 :(得分:24)
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
这展示了一些不同的概念:
$ntp_server_suffix
可以作为模板中的实例变量(@ntp_server_suffix
)进行访问<%
和%>
之间的代码由ruby执行<%=
和%>
之间的代码由ruby执行并输出<%=
和-%>
之间的代码并由ruby输出,并且尾部换行符被抑制。希望这可以帮助您理解模板。