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

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

如果任何人可以给我一个简单的例子,使用模板配置文件将是非常有帮助的.例如我如何设置Apache网站 – 使用模板的可用的配置文件,或者给出任何其他的例子,你会帮助一个新的木偶用户. BTW我在Ubuntu机器上.

解决方法

在2007年的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代码可以在erb模板中使用
><%和%之间的代码>由ruby执行
><%=和%>之间的代码由ruby执行并输出
><%=和 - %>之间的代码被ruby执行并输出,并且尾随的换行符被抑制.

希望这有助于您了解模板.

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...