Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 %>
share|improve this question
add comment

2 Answers 2

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 %>
share|improve this answer
    
Unfortunately "name" evaluates to the hostname of the server (from facter) –  Noodles Sep 25 '12 at 2: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 3:38
    
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 3:42
    
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 3:55
add comment

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.

share|improve this answer
    
Might be a bit of an overkill for such a small file –  Noodles Sep 25 '12 at 3:00
    
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 3:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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