”;
Puppet classes are defined as a collection of resources, which are grouped together in order to get a target node or machine in a desired state. These classes are defined inside Puppet manifest files which is located inside Puppet modules. The main purpose of using a class is to reduce the same code repetition inside any manifest file or any other Puppet code.
Following is an example of Puppet class.
[root@puppetmaster manifests]# cat site.pp class f3backup ( $backup_home = ''/backup'', $backup_server = ''default'', $myname = $::fqdn, $ensure = ''directory'', ) { include ''::f3backup::common'' if ( $myname == '''' or $myname == undef ) { fail(''myname must not be empty'') } @@file { "${backup_home}/f3backup/${myname}": # To support ''absent'', though force will be needed ensure => $ensure, owner => ''backup'', group => ''backup'', mode => ''0644'', tag => "f3backup-${backup_server}", } }
In the above example, we have two clients where the user needs to exist. As can be noticed we have repeated the same resource twice. One way of not doing the same task in combining the two nodes.
[root@puppetmaster manifests]# cat site.pp node ''Brcleprod001'',''Brcleprod002'' { user { ''vipin'': ensure => present, uid => ''101'', shell => ''/bin/bash'', home => ''/home/homer'', } }
Merging nodes in this fashion to perform the configuration is not a good practice. This can be simply achieved by creating a class and including the created class in nodes which is shown as follows.
class vipin_g01063908 { user { ''g01063908'': ensure => present, uid => ''101'', shell => ''/bin/bash'', home => ''/home/g01063908'', } } node ''Brcleprod001'' { class {vipin_g01063908:} } node ''Brcleprod002'' { class {vipin_g01063908:} }
The point to be noticed is how the class structure looks like and how we added a new resource using the class keyword. Each syntax in Puppet has its own feature. Hence, the syntax one picks depend on the conditions.
Parameterized Class
As in the above example, we have seen how to create a class and include it in a node. Now there are situations when we need to have different configurations on each node such as when one needs to have different users on each node using the same class. This feature is provided in Puppet using parameterized class. The configuration for a new class will look as shown in the following example.
[root@puppetmaster ~]# cat /etc/puppet/manifests/site.pp class user_account ($username){ user { $username: ensure => present, uid => ''101'', shell => ''/bin/bash'', home => "/home/$username", } } node ''Brcleprod002'' { class { user_account: username => "G01063908", } } node ''Brcleprod002'' { class {user_account: username => "G01063909", } }
When we apply the above site.pp manifest on nodes, then the output for each node will look like the following.
Brcleprod001
[root@puppetagent1 ~]# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppetagent1.testing.dyndns.org Info: Applying configuration version ''1419452655'' Notice: /Stage[main]/User_account/User[homer]/ensure: created Notice: Finished catalog run in 0.15 seconds [root@brcleprod001 ~]# cat /etc/passwd | grep "vipin" G01063908:x:101:501::/home/G01063909:/bin/bash
Brcleprod002
[root@Brcleprod002 ~]# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppetagent2.testing.dyndns.org Info: Applying configuration version ''1419452725'' Notice: /Stage[main]/User_account/User[bart]/ensure: created Notice: Finished catalog run in 0.19 seconds [root@puppetagent2 ~]# cat /etc/passwd | grep "varsha" G01063909:x:101:501::/home/G01063909:/bin/bash
One can also set the default value of a class parameter as shown in the following code.
[root@puppetmaster ~]# cat /etc/puppet/manifests/site.pp class user_account ($username = ‘g01063908''){ user { $username: ensure => present, uid => ''101'', shell => ''/bin/bash'', home => "/home/$username", } } node ''Brcleprod001'' { class {user_account:} } node ''Brcleprod002'' { class {user_account: username => "g01063909", } }
”;