Puppet - pass variable with a file create command

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?


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";
}

You can't define arbitrary meta parameters for the File resource like "thearch". The only meta parameters available are the ones here. You could use the existing architecture fact from the nodes which might give you the functionality you want.

<%= architecture %>

or perhaps

<% if architecture == 'i386' then -%>
  do some stuff
<% end-%>

You can't do that. Arguments to a resource are not arbitrary values. You could do this, though:

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