How to include a subtemplate in a Puppet template

2019-04-10 23:37发布

问题:

I'm trying to include a subtemplate in a template for one of my Puppet modules. Here's how I do the include in my base template:

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

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

This works fine if I render it with erb command-line tool. However, during the Puppet run the import is silently ignored, i.e. the base template is rendered with a blank line where the rendered subtemplate should be. There are no errors generated.

What am I doing wrong? Thanks!

回答1:

how about:

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


回答2:

argument to the function needs to be an array (>=puppet-3):

scope.function_foo(["bar"])

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



标签: erb puppet