【问题标题】:puppet erb dynamic content傀儡erb动态内容
【发布时间】:2019-01-07 19:02:57
【问题描述】:

网站.pp

class httpd_conf_files ($repos) {
    ensure_packages(['httpd'], {'ensure' => 'present'})
    $repos.each |String $repo| {
    file {"/etc/httpd/conf.d/${repo}_repo1.conf":
                                  ensure  => file,
                                  mode    => '0644',
                                      content => template('deploy/repos.erb'),
          }
      }
}

nodes.pp

node 'repo-web-c010' {
                  class { httpd_conf_files:
                           repos => ['centos','ubuntu'],
                  }
}

但是,centos_repo1.conf 和 ubuntu_repo1.conf 两个文件的内容相同。

repos.erb

    <% @repos.each do |rep|
    if rep == "centos"
        $x      = "/opt/repos/yum/"+rep
                 $_repo  = rep
         else
                 $x      = "/opt/repos/"+rep
                 $_repo  = rep
         end

    end -%>
    Alias /<%=$_repo%> <%=$x%>
    DocumentRoot  <%=$x%>
    IndexOptions NameWidth=* +SuppressDescription
    <Directory <%=$x%>>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog logs/<%=$_repo%>_repo1_error.log
    LogLevel warn

    CustomLog logs/<%=$_repo%>_repo1_access.log combined

谁能告诉我怎么了?

【问题讨论】:

  • 我不熟悉您的模板是如何被解析的,但是 @repos.each 块是否有可能在所有 repos 上运行,最后分配的值,解析模板?如果你颠倒你的 :repos 在notes.pp 中的顺序,那么这两个文件都有 centos 信息吗?您是否可以访问包含当前 $repo 的变量? HTH

标签: ruby puppet erb


【解决方案1】:

评论中暗示的 ERB 模板中的逻辑无效。

因为您循环遍历 @repos 数组并在每次迭代时设置 $x 和 $_repo,所以这些变量总是从该循环的最后一次迭代中获取它们的值。这就是为什么您总是以相同的生成内容结束。

模板可以改成这样:

<% if @repo == "centos"
     x = "/opt/repos/yum/" + @repo
     _repo = @repo
   else
     x = "/opt/repos/" + @repo
     _repo = @repo
   end -%>
Alias /<%= _repo %> <%= x %>
DocumentRoot  <%= x %>
IndexOptions NameWidth=* +SuppressDescription
<Directory <%= x %>>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

ErrorLog logs/<%= _repo %>_repo1_error.log
LogLevel warn

CustomLog logs/<%= _repo %>_repo1_access.log combined

请注意,我还更改了您的变量 $x 和 $_repo,因为美元符号表示 Ruby 中的全局变量,而全局变量可能不是您想要的。

如果您将条件逻辑从 ERB 模板移到 Puppet 清单中会更好。

最后,你真的需要修正你的缩进,因为它很难阅读。

【讨论】:

  • 感谢 Alex,我现在对如何使用 erb 模板有了更好的了解!
  • @johnnybravo,太好了,如果您对它解决了您的问题感到满意,请不要忘记接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多