【问题标题】:How to use a template for configuration file in Puppet如何在 Puppet 中使用模板作为配置文件
【发布时间】:2014-05-21 19:50:04
【问题描述】:

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

如果你们中的任何人都可以给我一个使用模板配置文件的简单示例,那将非常有帮助。例如,我如何使用模板设置 Apache 站点可用的默认配置文件,或者给出任何其他您认为可以帮助新 puppet 用户的示例。顺便说一句,我在 Ubuntu 机器上。

【问题讨论】:

    标签: linux templates configuration puppet


    【解决方案1】:

    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
    

    这展示了几个不同的概念:

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

    希望这有助于您理解模板。

    【讨论】:

    • 谢谢 ben..:)... 我查看了文档,它有点复杂。你有一个小例子可以推荐吗?谢谢
    • awesome ben.. 非常感谢.. 这真的让我很好地理解了模板的工作原理。我必须安装 ntp,然后我将它与你在 puppet 代码中修改它的方式进行了比较 :)...再次感谢..
    • 内容行应该是 content => template('ntp/ntp.conf.erb')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2011-02-24
    相关资源
    最近更新 更多