Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do modules use classes in puppet (instead of defines)

Tags:

puppet

I've been creating a couple of classes (in different modules) for puppet. Both, separately, require maven. So both classes have something like the following:

    class { "maven::maven":
        version => "3.0.5"
    }

(using the https://forge.puppetlabs.com/maestrodev/maven module from puppet forge)

But, if I have one node that has both of my classes, puppet complains because class 'maven::maven' is declared twice. I feel like each of my classes should be free to declare all of the things it needs. If a node has more than one class both of which require maven, then I don't see the problem.

So, my question is: was the author of that maven module wrong to use a class, should he have used a define instead? (because you can use/call/whatever a define multiple times). It appears that if he had used a define I would be able to have the block of code as many times as I like, so if he was right to use a class, why?

Thanks.

like image 679
stripybadger Avatar asked Nov 18 '13 16:11

stripybadger


People also ask

What is the benefit of grouping resources into classes when using puppet?

Puppet uses classes to make the structure reusable and organized. Classes can only be evaluated once per node. Classes are described within the manifest file, located inside Puppet modules. The main reason for using a class is to decrease the duplication of the same code inside any manifest file or other puppet code.

What are classes in puppet?

Classes are named blocks of Puppet code that are stored in modules and applied later when they are invoked by name. You can add classes to a node's catalog by either declaring them in your manifests or assigning them from an external node classifier (ENC).

What are modules in puppet?

Puppet modules are a collection of manifests and data, which can include facts, files, and templates. Modules help you organize and reuse Puppet code by enabling you to split the code into several manifests. With the exception of the main site.

Is it compulsory to precede class parameters with its data type in puppet?

Each parameter can be preceeded by an optional data type. If you include one, Puppet will check the parameter's value at runtime to make sure that it has the right data type, and raise an error if the value is illegal. If no data type is provided, the parameter will accept values of any data type.


1 Answers

I think the rationale behind this is best explained in John Arundel's Puppet 3 Beginner's Guide:

So if you're wondering which to use, consider:

  • Will you need to have multiple instances of this on the same node (for example, a website)? If so, use a definition.
  • Could this cause conflicts with other instances of the same thing on this node (for example, a web server)? If so, use a class.
like image 89
Evgeny Chernyavskiy Avatar answered Dec 17 '22 01:12

Evgeny Chernyavskiy