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.

Is there a way to (a) accept arbitrary arguments to a class or resource definition and then (b) pass those arguments on to another class or resource definition?

I often find that what I am writing is a thin wrapper over a native type or over a third-party module. For example:

define myconfigfile {
  file { "/path/to/$name":
    owner => lars,
    group => lars,
    ...
  }
}

That is, myconfigfile is effectively shorthand for a set of file resources with a known prefix. I would actually like myconfigfile to accept the same parameters as the file resource type (such as content, source, etc), but I would like to avoid:

define myconfigfile (
  $content = undef,
  $source = undef
) {
  ...
}

This works okay when there are only one or two parameters, but it becomes unmanageable if I am trying to wrap a parameterized class with lots of parameters.

share|improve this question

1 Answer 1

One possibility would be to use a hash to pass in an arbitrary list of parameters.

Another, and perhaps better option would be to make use of the Ruby DSL which allows you to use Ruby instead of the Puppet language.

There are a variety of examples available as well.

Essentially, you would pass in the hashmap then use Ruby's iteration/looping (the ruby hash object has a .each method) to extract the key-value pairs from the hash.

The Ruby DSL became available in 2.6.x.

share|improve this answer
    
And then what? I've used hash parameters for templates in the past, but I'm not sure how to best make use of them for the situation described in my question. –  larsks Jun 28 '12 at 13:39
    
You'd have to know what was passed in, so it would be somewhat difficult. It's a shame that puppet doesn't have better iteration in it's language, outside of templates. –  Lee Lowder Jun 28 '12 at 17:58
    
Updated the answer, so it actually provides an solution now. –  Lee Lowder Jun 29 '12 at 15:55
    
The Ruby DSL has been deprecated. –  chutz Apr 2 at 11:38

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.