4

In the module I'm currently working on, I got a load of configuration options that need to be set, have default values ... and should be fragmented into shorter template files as else it would be a scrolling nightmare.

The recommended way to do so was to use some third party modules. Is there no built in way?

kaiser
  • 1,251
  • 1
  • 16
  • 24

2 Answers2

7

Another possible solution that surprisingly works (and about which I couldn't find any documentation), is the array syntax inside the template() function:

file { "${location}/final-file.php":
    ensure  => file,
    content => template(
        'wppuppet/template.a.erb',
        'wppuppet/template.b.erb',
        'wppuppet/template.c.erb'
    ),
}
kaiser
  • 1,251
  • 1
  • 16
  • 24
  • Cool! How did you find it out? – Adam Ryczkowski May 03 '14 at 06:30
  • Assumption > Try > Error > Repeat and a lot of patience in between :) ... My IDE only supports PHP and JS :P – kaiser May 03 '14 at 06:54
  • 1
    This is in the puppetlabs documentation: http://docs.puppetlabs.com/guides/templating.html#combining-templates – daxlerod May 03 '14 at 15:49
  • @daxlerod then I overread that part :P Wonder why those modules then are so popular. – kaiser May 03 '14 at 16:57
  • 1
    The modules typically offer some more flexibility, you may not know ahead of time where your fragments will come from. Also, take a look at how the mcollective::plugin type in the puppetlabs-mcollective module uses datacat to combine multiple directories. https://github.com/puppetlabs/puppetlabs-mcollective – daxlerod May 03 '14 at 21:10
  • Just an FYI, the template function doesn't accept array variables. See https://projects.puppetlabs.com/issues/8492 for details. This becomes a problem if you want to use hiera to configure your list. – BillMan May 19 '15 at 17:50
5

One possible solution is to fetch templates in variables. Then concatenate the string and push it into an inline_template():

$a = template( 'wppuppet/my-file.a.erb' )
$b = template( 'wppuppet/my-file.b.erb' )
$c = template( 'wppuppet/my-file.c.erb' )

file { "${location}/final-file.php":
    ensure  => file,
    content => inline_template( "${a}${b}${c}" ),
}
kaiser
  • 1,251
  • 1
  • 16
  • 24