Puppet – pass variable with a file create command

puppet

I need a way to pass a given variable – lets say thearch – to several different files within a given class. I need to be able to state the contents of this variable for each file individually.

I have tried the following:

file { "xxx":
  thearch => "i386",
  path    => "/xxx/yyyy",
  owner   => root,
  group   => root,
  mode    => 644,
  content => template("module/test.erb"),
}

This doesn't pass this variable so I can use it with a <%=thearch%> statement within the erb file as I expect.

What am I doing wrong here?

Best Answer

You'll need to wrap the file in a define that does take that parameter, so that it's available when the template is called, and then call that define. If a lot of the parameters are usually the same, set them as defaults while you're at it just to keep the code clean.

define thearch_file($thearch, $path, $owner = root, $group = root, $mode = 0644, $template = '/module/test.erb') {
  file { $name:
    path    => $path,
    owner   => $owner,
    group   => $group,
    mode    => $mode,
    content => template($template),
  }
}

thearch_file {
  "xxx":
    thearch => 'i386',
    path    => "/xxx/yyy";
  "yyy":
    thearch => 'x86_64',
    path    => "/xxx/zzz";
}