7

i try to create two different files with one template, because they only diff by one line.

file 
{
    "/tmp/bootstrap-raid.sh":
    content => template("pxe/bootstrap.sh.erb"),
}

file 
{
    "/tmp/bootstrap-noraid.sh":
    content => template("pxe/bootstrap.sh.erb"),
}

bootstrap.sh.erb:

<% if ??? == "???" %>
-r yes \
<% else %> 
-r no \
<% end %>

i can't pass a variable by defining it twice like $raid=yes file{} $raid=no file{}, so i tried to define the variable inside each file{} with no effort. then i thought about using the targetfilename inside the template like <% if filename == "/tmp/bootstrap-raid.sh" %> which is also not possible.

how to work call a template twice with different "parameters"

my target is to not defining and calling an extra function in the manifest file, or making two templates. any ideas?

are there any predefined default variables in a template like the filename of the targetfile, the templatename,...?

edit: another example would be having two php.ini files like in debian, one for the command line and one for the webserver. i want only to exchange the memory limit. but each server needs both php.ini files. i am looking for a way to pass a hardcoded parameter to the template file or a way that i can if/then/else based upon the targetfilename. of course i know that i am able to create a new define, which i can call twice. but i am looking for an easier way.

2 Answers 2

1

You can't get the resource name (it seems), but you can get the class. For example:

class bootstrap-raid {
    file {
        "/tmp/bootstrap-raid.sh":
        content => template("/root/bootstrap.sh.erb"),
    }
}
class bootstrap-noraid {
    file {
        "/tmp/bootstrap-noraid.sh":
        content => template("/root/bootstrap.sh.erb"),
    }
}

And for the template:

<% if name == "bootstrap-raid" %>
-r yes \
<% else %> 
-r no \
<% end %>

There's also title available, which usually means the same thing (at least, I never saw them differ).

1
  • thank you for the answer, you solution is not i am looked for, but your answer is correct, ist simply not possible. for getting it run i switched to defines.
    – c33s
    Nov 28, 2011 at 5:17
7

Perhaps you should do this with a define?

class bootstrap {
 define conf ( $israid= undef ) {
  $loc = $name
  file {
    "$loc":
      content => template(blah.erb);
  }
 }
}       

then call it with

include bootstrap
  bootstrap::conf {
    "/loc":
      israid => '-r yes \';
    }
  bootstrap::conf {
    "/otherloc":
      israid => '-r no \';
    }

your template will need to handle the israid variable you can just use an include and keep the logic out of the template (or at least less of the logic in there

 <%= israid %>
3
  • thank you for answering, but its not what i am looking for. added clearification.
    – c33s
    Oct 18, 2011 at 4:51
  • Why don't you want to use a define?
    – Tacticus
    Oct 18, 2011 at 5:32
  • 1
    a (pseudocode) template("templatename.erb",'myvar' => 12) looks more handy to me than having to add a define for each template i want to use twice. i think there must be a simple way to access the targetfilename or to easily pass a variable in.
    – c33s
    Oct 18, 2011 at 6:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .