Ask Your Question
0

What is the difference between this class syntax in puppet 3.8 ?

asked 2016-01-25 14:43:41 -0500

greywindow gravatar image

Hello all, I'm just trying to understand what is the difference between code like this:

include ntp

versus

class { "ntp": }

and when is better to use one from the other?

Thank you

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-01-25 15:51:32 -0500

lavaman gravatar image

updated 2016-01-25 15:53:00 -0500

You can include/require classes in multiple places in the code, but you can only declare a class in one place in the code. Also, you typically only declare classes when you are passing them parameters in your puppet code (as opposed to autolookup in hiera or via an enc).

edit flag offensive delete link more

Comments

OK, here's an example:
class host1 { include ntp }
versus
host1 { class { "ntp": } }
If I understand this correctly then the only difference between these 2 is that in the second piece of code we can pass parameters, right?
Thank you

greywindow gravatar imagegreywindow ( 2016-01-26 09:17:00 -0500 )edit

That is one difference. The other (and probably more important one) is that you can only do the second once ANYWHERE in the code that gets applied to a node. When you have modules that call other modules, this becomes an issue rather quickly.

lavaman gravatar imagelavaman ( 2016-01-26 13:03:51 -0500 )edit
0

answered 2016-02-07 18:46:29 -0500

Henrik Lindberg gravatar image

The resource expression class declaration can only be evaluated once whereas you can do an include as may times as you like. You use the resource expression class if you want/need to set parameters from your manifest. It is highly recommended to use hiera and automatic data binding for this as that enables you to always just include the class. If you do use the resource expression class declaration expression, you can include that class (without parameters) as many times as you like - provided that you first define it with parameters. (Done in some other order it will ail with an error).

There is one more difference. When you use include, the class is immediately evaluated, but when using the resource expression class declaration the evaluation is queued, and the class is defined at a later point in time. This only matters if you are using logic in your manifests that are "evaluation order dependent" (there are only a few of them, and the documentation for those operations should clearly say that it depends on the order of evaluation (often referred to in error as "parse order")

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-01-25 14:43:41 -0500

Seen: 211 times

Last updated: Jan 25 '16