Learning Kit

Puppet Language Basics

Classes

Time: 5 minutes
Materials needed: none

Resources can be collected, combined, and distributed together using classes. By combining resources into a class, a class can configure large blocks of functionality, containing all of the packages, configuration files, and services needed to run an application.
A single manifest should contain only one class definition each. A single class can be used across multiple nodes, but each class name you use must be unique and, once declared, a class cannot be declared again on the same node.

class class_name {
	resource_type { 'resource_name':
		attribute => value,
	}
	resource_type { 'resource_name':
		attribute => value,
	}
	resource_type { 'resource_name':
		attribute => value,
	}
}

Classes begin with the class keyword, followed by the name of the class. Then, you’ll write all of the classes’ resources within {} (curly braces). Each resource inside a class follows normal resource declaration syntax.

class ssh {
	package { 'openssh-clients':
		ensure  => present,
	}
	file { '/etc/ssh/ssh_config':
		ensure  => file,
		content => template ('ssh/ssh_config'),
	}
	service { 'sshd':
		ensure  => running,
		enable  => true,
	}
}

This example class definition contains package, file, and service resources. When this class is distributed to a node, the node will receive all of these configurations together at once.

Defining a class specifies the contents and behaviors of that class, but it doesn’t automatically add the class to your configuration. To do that, the class must be declared. Declaring a class adds each resource to the catalog and directs Puppet to begin managing those resources.

include class _name 

or

class {'class_name':}

There are two ways to declare a class in a manifest: by using the include keyword followed by the name of the class, or by using the class keyword followed by the name of the class in (single quotes), followed by a : (colon), and enclosed in {} (curly braces).

Puppet sites use proprietary and third-party cookies. By using our sites, you agree to our cookie policy.