How to use Classes in Puppet

Last updated on May 28 2022
Satyen Sahu

Table of Contents

How to use Classes in Puppet

Puppet – Classes

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",

   }

}

 

So, this brings us to the end of blog. This Tecklearn ‘How to use Classes in Puppet’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Puppet and build a career in DevOps domain, then check out our interactive, Continuous Deployment: Configuration Management using Puppet Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/continuous-deployment-configuration-management-using-puppet/

Continuous Deployment: Configuration Management using Puppet Training

About the Course

Tecklearn has specially designed this Continuous Deployment: Configuration Management using Puppet Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of Configuration Management using Puppet, Puppet Components, important concepts like Puppet Lifecycle, Puppet Language and Puppet Installation. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Puppet.

Why Should you take Configuration Management using Puppet Training?

  • Average salary of Puppet Professional is $90k – Payscale.com
  • Uber, Salesforce, PayPal, Booking.com, MIT, Starbucks. & many other MNC’s worldwide use Puppet across industries.
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Continuous Deployment: Configuration Management using Puppet

  • Need of Configuration Management
  • What is Puppet
  • Puppet Architecture
  • Puppet Components
  • Puppet Lifecycle
  • Setting up Master Slave using Puppet
  • Puppet Manifests
  • Puppet Modules
  • Applying configuration using Puppet
  • Puppet File Server
  • Hands On

 

0 responses on "How to use Classes in Puppet"

Leave a Message

Your email address will not be published. Required fields are marked *