4

I have a file that I'd like to reuse for a few different purposes. The file is 90% the same across uses, just slight differences. I'd rather not replicate the content across multiple files in puppet, so is there a way to do something like

file { "/tmp/file1" :
  content => template("module/template.erb")
}

file { "/tmp/file2" :
  content => template("module/template.erb")
}

And in the template:

Jack
John
James
<% if file == "/tmp/file2" %>
Jim
<% end %>
Noodles
  • 1,356
  • 3
  • 18
  • 29

2 Answers2

5

You should use a define or a parametrized class, that way you can get name to what you like (IMHO, should be a define):

define filename($template = "mytemplate.erb") {
  file { $name:
    content => template($template)
  }
}

node 'host' {

  filename { "/tmp/file1": }
  filename { "/tmp/file2": }
}

And correct your template to:

Jack
John
James
<% if name == "/tmp/file2" %>
Jim
<% end %>
Torian
  • 2,314
  • 18
  • 10
  • Unfortunately "name" evaluates to the hostname of the server (from facter) – Noodles Sep 25 '12 at 02:57
  • You are absolutely right. I missed the part where that should go on a define(), so you can get `name` to the actual path of the file. I corrected the answer. – Torian Sep 25 '12 at 03:38
  • 1
    wouldnt it be better to use a variable of something like "filepath" rather than mask a facter variable in this way ? Seems to me like obscurity without gain ? – Sirex Sep 25 '12 at 03:42
  • 1
    The problem to that, is that you cannot redefine a variable in the same context. That means that if you want to call `file { $a: ... }` and later, in the same node definition (or class or whatever) you want the same `file { $a: ... }` but with a previous redefine of `$a` you'll get a manifest compilation error. – Torian Sep 25 '12 at 03:55
0

It sounds like you want to build a config file from fragments ?

http://projects.puppetlabs.com/projects/puppet/wiki/Generating_a_config_file_from_fragments

I've not tried this yet, but I want to. Let me know how it goes if you try this.

Sirex
  • 5,447
  • 2
  • 32
  • 54
  • Might be a bit of an overkill for such a small file – Noodles Sep 25 '12 at 03:00
  • 1
    There's no such thing as overkill ?! Seriously though, I thought that the first time i ran into this issue, and 4-5 times now I've avoided it when I should just take the plunge. – Sirex Sep 25 '12 at 03:35