Search code examples
puppet

Is Include and Class Both Needed for using Puppet Module


I am trying to use the ntp puppet module. The instructions say to use "include" to install and enable, but then use "class" to configure the parameters. Is the "include" even needed?

Setup
Beginning with ntp

include ntp is enough to get you up and running. To pass in parameters specifying which servers to use:

class { 'ntp':
  servers => [ 'ntp1.corp.com', 'ntp2.corp.com' ],
}

Usage

All parameters for the ntp module are contained within the main ntp class, so for any function of the module, set the options you want. See the common usages below for examples.
Install and enable NTP

include ntp

Change NTP servers

class { 'ntp':
  servers => [ 'ntp1.corp.com', 'ntp2.corp.com' ],
}

I am confused if I need to always use "include ntp" before using the "class { 'ntp':" or if it is sufficient to not use the "include ntp" and ONLY use the "class { 'ntp':" when using the module in my site.pp

My question are:

1) When is it necessary to use just an "include" and no "class" statement ... is this only needed if we are using the module but not specifying any parameters?

2) Is it allowed to exclude the "include" statement, and just use the "class" statement? i.e. is it valid to not use the "include" statement, since the class statement will essentially do the same thing as the include statement, but will also included the configured parameters? Isn't the "include" statement redundant?

3) When should I use BOTH the "include" statement and the "class" statement?

I can't see any case where using the "include" statement is needed, since the class statement does the same thing as the include statement.


Solution

  • This is well documented at https://puppet.com/docs/puppet/latest/lang_classes.html#reference-4789.

    To answer your questions

    1. Yes, you only need to use class when you are specifying parameters
    2. Yes, if you've declared using class you shouldn't also declare using include
    3. Don't use both include and class in the same manifest. With care, you can mix a number of include declarations with one class declaration across several manifests, but within a single manifest, just use one or the other