I'd like to use template() to concatenate some files. I read this question, which is fine if you know exactly how many files you want to concat. What if I just have an array of input file names?

$files = ['mymod/a.erb', 'mymod/b.erb', 'mymod/c.erb']

file { "/var/foo/final":
  content => template ($files)  # <-- error, can't convert Array to String

I'd like to avoid having to write a parser function.


This is not possible at the moment. There was a Puppet issue submitted for it and a fix proposed but does not seem like there's any rush to implement it.

As a possible workaround, you could have one template include others:

<% @template_array.each do |val| -%>
<% scope.function_template(val) %>
<% end -%>

Forewarning, I have not tested this but I believe it should work.

  • I will try it out and report back. I was going to ask if it's possible to "call" the puppet template() function from inside Proper Ruby. If so, you could actually skip the extra file and go straight for inline_template('@arr.reduce ("") { |str, f| str + scope.function_template(f) }') – Coderer Mar 30 '15 at 9:32
  • Oh, good idea. I tend to go with separating logic and presentation so I avoid inline_template but this is a use case that may be appropriate. – Belmin Fernandez Mar 30 '15 at 12:17
  • If your manifest does this a lot (e.g. in a defined type that is instantiated in large numbers), this will cause a measurable performance hit. Apart from some boiler plate, a custom function will really not be that much more code, but saves CPU. – Felix Frank Mar 30 '15 at 12:24
  • Thanks for the advice, @FelixFrank, I will keep it in mind. It's for a monitoring module, so I can keep "things to monitor" in partial config files and assign those on a per-host basis. The module should be instantiated once per host, so I think a performance hit should be acceptable. – Coderer Mar 30 '15 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.