Skip to content

Latest commit

 

History

History
72 lines (55 loc) · 1.63 KB

File metadata and controls

72 lines (55 loc) · 1.63 KB

Classes

← Back to Language Reference


Classes are named blocks of Puppet code that can be declared (included) on a node. They're the primary way to organize your Puppet code.

Defining a Class

# modules/webserver/manifests/init.pp
class webserver (
  Integer        $port    = 80,
  Boolean        $ssl     = false,
  Array[String]  $modules = ['mod_ssl', 'mod_rewrite'],
) {
  package { 'httpd':
    ensure => installed,
  }

  file { '/etc/httpd/conf/httpd.conf':
    ensure  => file,
    content => epp('webserver/httpd.conf.epp', {
      'port'    => $port,
      'ssl'     => $ssl,
      'modules' => $modules,
    }),
    notify  => Service['httpd'],
  }

  service { 'httpd':
    ensure => running,
    enable => true,
  }
}

Declaring (Using) a Class

# Method 1: include (uses Hiera for parameters)
include webserver

# Method 2: resource-like declaration (sets parameters explicitly)
class { 'webserver':
  port => 8080,
  ssl  => true,
}

Best practice: Use include with Hiera data for parameters. Resource-like declarations (class { ... }) can only be used once per class and are less flexible.

The contain and require Functions

class myapp {
  contain myapp::install   # Include + contain within this class's scope
  contain myapp::config
  contain myapp::service

  Class['myapp::install']
    -> Class['myapp::config']
    ~> Class['myapp::service']
}

← Back to Language Reference

This document was created with the assistance of AI (Grok, xAI). All technical content has been reviewed and verified by human contributors.