Writing Puppet Classes

Classes are blocks of Puppet code that enable reuse. Using classes makes reading manifests less complicated. A class definition contains the code for a specific class. You first define the class, then you make the class available for use within manifests. Note that the class itself does not perform any evaluation.

The following example shows the format that is used for a class definition named examplecloud:

class examplecloud::analytics {

    package { "system/management/webui/webui-server":
        ensure => installed,
    }

    svccfg { "webui":
        require => Package["system/management/webui/webui-server"],
        fmri => "system/webui/server:default",
        property => "conf/redirect_from_https",
        value => "false",
        ensure => present,
    }

    service { "system/webui/server":
        require => Package["system/management/webui/webui-server"],
        ensure => running,
    }

}

This example class has the examplecloud and analytics name spaces. The code in this class ensures that the following occurs on the node:

  • Installs certain IPS packages.

  • Applies a certain SMF configuration prior to enabling the analytics SMF service.

A class declaration in a manifest instructs Puppet to evaluate the code within that class. A class declaration is itself a class that is defined within a manifest.

Puppet defines the normal and resource-like class declarations.

  • The following normal class declaration example includes the include keyword in the Puppet code:

    include example_class
  • The following resource-type class declaration example declares the class in a similar way as a resource declaration:

    class { 'example_class': }

    Use resource-like class declarations to specify class parameters that override the default values of class attributes (or parameters).

For more information about writing and assigning Puppet classes, go to https://puppet.com/docs/puppet/7/lang_classes.html.

Including a Class Declaration in a Puppet Manifest

The following example manifest uses the examplecloud class declaration in the /etc/puppetlabs/code/modules directory on the Puppet Server (server).

The examplecloud class contains several manifests (/etc/puppetlabs/code/modules/examplecloud/manifests) that specify various configurations. The following example shows that each manifest includes the examplecloud class declaration:

# NTP configuration for companyfoo
class examplecloud::ntp {

    file { "ntp.conf" :
        path => "/etc/inet/ntp.conf",
        owner => "root",
        group => "root",
        mode => 644,
        source => "puppet:///modules/examplecloud/ntp.conf",
    }

    package { "ntp":
        ensure => installed,
    }

    service { "ntp":
        require => File["ntp.conf"],
        subscribe => File["ntp.conf"],
        ensure => running,
    }

}

The examplecloud class declarations in the previous example ensure the following:

  • Installs the NTP package

  • Installs a certain configuration file that is sourced from a location other than the server

  • Enables the NTP service and puts it in a running state on the node