繁体   English   中英

如何在Puppet中使用模板配置文件

[英]How to use a template for configuration file in Puppet

我是Puppet的新手,我正在编写一个模块来设置配置文件。 问题是当多个客户端将使用我的模块时,他们将不得不根据他们的系统进行编辑。 我听说模板是解决这个问题的方法。 但我无法得到如何使用模板来设置配置文件。

如果你们中的任何人都可以给我一个简单的例子,使用模板来配置文件会非常有帮助。 例如,如何使用模板设置Apache站点 - 可用的默认配置文件,或者给出您认为有助于新木偶用户的任何其他示例。 BTW我在Ubuntu机器上。

关于使用Puppet模板的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

这展示了一些不同的概念:

  1. puppet清单中定义的变量(例如$ntp_server_suffix可以作为模板中的实例变量( @ntp_server_suffix )进行访问
  2. 可以在erb模板中使用循环和其他ruby代码
  3. <%%>之间的代码由ruby执行
  4. 执行<%=%>之间的代码并由ruby输出
  5. 执行<%=-%>之间的代码并由ruby输出,并且抑制尾随换行符。

希望这可以帮助您理解模板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM