5

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?

5
  • ... and your erb file contains something like <%= @thearch %>? Just checking, your example lacks @ from <%=thearch%> ... – Janne Pikkarainen Jun 5 '12 at 18:03
  • I tried a couple variants, I'll double check that specifically. – Tim Brigham Jun 5 '12 at 18:08
  • Using the @ returns "Invalid parameter". – Tim Brigham Jun 5 '12 at 18:19
  • Strange. Have you read this? docs.puppetlabs.com/guides/templating.html – Janne Pikkarainen Jun 5 '12 at 18:20
  • Repeatedly, unfortunately it doesn't seem to cover my needs. – Tim Brigham Jun 5 '12 at 18:31
4

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

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-%>
2
  • That is kind of what I figured, however the architecture facts don't work in this situation (the given host is providing resources for multiple architectures). – Tim Brigham Jun 5 '12 at 18:49
  • You might want to create a new question explaining what you're trying to do. It isn't very clear from the above. I'd recommend a define in general, but it's not clear how you're deciding or why the current facts won't work. – kashani Jun 5 '12 at 18:59
1

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"),
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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