Puppet Classes
• Classes are named blocks of Puppet code that are stored in modules for later use and are not applied until they are invoked by name
• They can be added to a node’s catalog by either declaring them in your manifests or assigning them from an ENC
• Classes generally configure large or medium-sized chunks of functionality, such as all of the packages, config files, and services needed to run an application
• Classes are containers of different resources. Since Puppet 2.6 they can have parameters
Example of Class
class mysql ( root_password => 'default_value', port => '3306', )
{ package { 'mysql-server': ensure => present, }
service { 'mysql': ensure => running, } [...] }
Note : that when we define a class we just describe what it does and what parameters it has, we don't actually add it and its resources to the catalog.
Defining Classes
# A class with no parameters
class base::linux
{ file { '/etc/passwd': owner => 'root', group => 'root', mode => '0644', }
file { '/etc/shadow': owner => 'root', group => 'root', mode => '0440', }
}
Defining Classes
# A class with parameters
class apache (String $version = 'latest')
{ package {'httpd': ensure => $version, # Using the class parameter from above before => File['/etc/httpd.conf'], }
file {'/etc/httpd.conf': ensure => file, owner => 'httpd', content => template('apache/httpd.conf.erb'), # Template from a module }
service {'httpd': ensure => running, enable => true, subscribe => File['/etc/httpd.conf'], }
}
Class Parameters & Variables
• Parameters allow a class to request external data. If a class needs to configure itself with data other than facts, that data should usually enter the class via a parameter
• Each class parameter can be used as a normal variable inside the class definition
• The values of these variables are not set with normal assignment statements or looked up from top or node scope; instead, they are set based on user input when the class is declared
• If a class parameter lacks a default value, the module’s user must set a value themselves
• You should supply defaults wherever possible
• Each parameter can be preceded by an optional data type
• If included, Puppet will check the parameter’s value at runtime to make sure that it has the right data type, and raise an error if the value is illegal
• If no data type is provided, the parameter will accept values of any data type
• The special variables $title and $name are both set to the class name automatically, so they can’t be used as parameters
Class Location
• Class definitions should be stored in modules. Puppet is automatically aware of classes in modules and can autoload them by name.
• Classes should be stored in their module’s manifests/ directory as one class per file, and each filename should reflect the name of its class
• A class definition statement isn’t an expression and can’t be used where a value is expected.
Comments
Post a Comment