【问题标题】:How to include a subtemplate in a Puppet template如何在 Puppet 模板中包含子模板
【发布时间】:2013-05-14 19:51:54
【问题描述】:

我正在尝试在我的一个 Puppet 模块的模板中包含一个子模板。以下是我在基本模板中包含的方法:

<%
  def import(fname)
    erb =
      File.open(File.dirname(__FILE__) + "/" + fname) { |fp| ERB.new(fp.read) }
    erb.run
  end
%>

<%= import("subtemplate.erb") %>

如果我使用 erb 命令行工具渲染它,这会很好。但是,在 Puppet 运行期间,导入被静默忽略,即基本模板在渲染子模板应该存在的地方用空行渲染。没有产生错误。

我做错了什么?谢谢!

【问题讨论】:

    标签: erb puppet


    【解决方案1】:

    函数的参数需要是一个数组 (>=puppet-3):

    scope.function_foo(["bar"])
    

    http://docs.puppetlabs.com/guides/templating.html#using-functions-within-templates

    【讨论】:

      【解决方案2】:

      怎么样:

       <%= scope.function_template("subtemplate.erb") %>
      

      【讨论】:

      • 感谢您为我指明正确的方向!您的建议没有立即奏效,但我能够在此基础上进行构建并最终得到:&lt;%= scope.function_template(File.dirname(__FILE__) + "/" + "subtemplate.erb") %&gt; 我必须预先添加 pwd 的原因可能是因为原始模板嵌套在 modules/templates 目录中的几个子目录中.
      • 感谢您的回答!我有一个快速的问题;有没有办法避免 subtemplate.erb 文件的标头注释附加到调用 erb?为此,我们需要为 subtemplate.erb 中的许可标头使用什么格式?谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2015-09-13
      • 2012-08-15
      • 2011-11-16
      • 2014-03-12
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多