Passing parameters to classes

Sometimes it's very useful to parameterize some aspect of a class. For example, you might need to manage different versions of a gem package, and rather than making separate classes for each that differ only in the version number, or using inheritance and overrides, you can pass in the version number as a parameter.

How to do it…

  1. Declare the parameter as part of the class definition as follows:
    class eventmachine( $version ) {
        package { "eventmachine":
            provider => gem,
            ensure   => $version,
        }
    }
  2. Then use the following syntax to include the class on a node:
    class { "eventmachine": version => "0.12.8" }

How it works…

The class definition:

class eventmachine( $version ) {

is just like a normal class definition except it specifies ...

Get Puppet 2.7 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.