[英]How to use a template for configuration file in Puppet
I am new to Puppet and I am writing a module to setup configuration files. 我是Puppet的新手,我正在编写一个模块来设置配置文件。 The problem is when multiple clients will be using my module they will have to edit it according to their system. 问题是当多个客户端将使用我的模块时,他们将不得不根据他们的系统进行编辑。 I have heard that templates are way to solve this problem. 我听说模板是解决这个问题的方法。 But I am not able to get it how to use a template for setting up configuration file. 但我无法得到如何使用模板来设置配置文件。
If anyone of you can give me a simple to follow example using templates to configure files would be really helpful. 如果你们中的任何人都可以给我一个简单的例子,使用模板来配置文件会非常有帮助。 For example how can i setup Apache sites-available default configuration file using template, or give any other example you feel will help a new puppet user. 例如,如何使用模板设置Apache站点 - 可用的默认配置文件,或者给出您认为有助于新木偶用户的任何其他示例。 BTW I am on Ubuntu machine. BTW我在Ubuntu机器上。
The PuppetLabs docs on Using Puppet Templates has an example of an Apache configuration for a Trac site. 关于使用Puppet模板的PuppetLabs文档有一个Trac站点的Apache配置示例。 This should be enough to get you started. 这应该足以让你入门。
Per OP's request, here's a simple example. 根据OP的要求,这是一个简单的例子。 I'm using NTP rather than the Apache default config since that's a fairly large and complex file. 我正在使用NTP而不是Apache默认配置,因为这是一个相当大而复杂的文件。 NTP is much simpler. NTP更简单。
Directory looks like this: 目录看起来像这样:
/etc/puppet/modules/ntp/manifests
/templates
Partial contents /etc/puppet/modules/ntp/manifests/init.pp
(just the portion defining the template): 部分内容/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,
}
Contents of /etc/puppet/modules/ntp/templates/ntp.conf.erb
: /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
When run with puppet this will result in an /etc/ntp.conf
that looks like: 当使用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
This demonstrates a few different concepts: 这展示了一些不同的概念:
$ntp_server_suffix
can be accessed as instance variables ( @ntp_server_suffix
) in the template puppet清单中定义的变量(例如$ntp_server_suffix
可以作为模板中的实例变量( @ntp_server_suffix
)进行访问 <%
and %>
is executed by ruby <%
和%>
之间的代码由ruby执行 <%=
and %>
is executed and output by ruby 执行<%=
和%>
之间的代码并由ruby输出 <%=
and -%>
is executed and output by ruby and the trailing newline character is suppressed. 执行<%=
和-%>
之间的代码并由ruby输出,并且抑制尾随换行符。 Hope this helps you understand templates. 希望这可以帮助您理解模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.