Concept of Type and Provider in Puppet

Last updated on May 27 2022
Sarika Tak

Table of Contents

Concept of Type and Provider in Puppet

Puppet – Type and Provider

Puppet types are used for individual configuration management. Puppet has different types like a service type, package type, provider type, etc. Wherein each type has providers. The provider handles the configuration on different platforms or tools. For example, the package type has aptitude, yum, rpm, and DGM providers. There are a lot of types and Puppet covers a good spectrum configuration management item that needs to be managed.

Puppet uses Ruby as its base language. All Puppet types and providers present are written in Ruby language. As it follows the standard encoding format, one can simply create them as shown in the example for repo which manages repositories. Here, we will create type repo and providers’ svn and git. The first part of the repo type is type itself. The types are usually stored in lib/puppet/type. For this, we will create a file called repo.rb.

$ touch repo.rb

Add the following content in the file.

Puppet::Type.newtype(:repo) do

@doc = “Manage repos”

Ensurable

newparam(:source) do

desc “The repo source”

 

validate do |value|

if value =~ /^git/

resource[:provider] = :git

else

resource[:provider] = :svn

end

end

isnamevar

end

 

newparam(:path) do

desc “Destination path”

validate do |value|

unless value =~ /^\/[a-z0-9]+/

raise ArgumentError , “%s is not a valid file path” % value

end

end

end

end

In the above script, we have created a block “Puppet::Type.newtype(:repo) do” which creates a new type with the name repo. Then, we have @doc which helps in adding whatever level of details one wants to add. The next statement is Ensurable; it creates a basic ensure property. Puppet type uses ensure property to determine the state of configuration item.

Example

service { “sshd”:

ensure => present,

}

The ensure statement tells Puppet to except three method: create, destroy, and exist in the provider. These methods provide the following features −

  • A command to create a resource
  • A command to delete a resource
  • A command to check the existence of a resource

All we then need to do is specify these methods and their contents. Puppet creates the supporting infrastructure around them.

Next, we define a new parameter called source.

newparam(:source) do

desc “The repo source”

validate do |value|

if value =~ /^git/

resource[:provider] = :git

else

resource[:provider] = :svn

end

end

isnamevar

end

The source will tell the repo type where to retrieve/clone/checkout the source repository. In this, we are also using a hook called validate. In the provider section, we have defined git and svn which check for the validity of the repository we have defined.

Finally, in the code we have defined one more parameter called path.

newparam(:path) do

desc “Destination path”

validate do |value|

unless value =~ /^\/[a-z0-9]+/

raise ArgumentError , “%s is not a valid file path” % value

end

This is the value type which specifies where to put the new code that is retrieved. Here, again use the validate hook to create a block that checks the value of appropriateness.

Subversion Provider Use Case

Let’s start with the subversion provider using the above created type.

require ‘fileutils’

Puppet::Type.type(:repo).provide(:svn) do

desc “SVN Support”

 

commands :svncmd => “svn”

commands :svnadmin => “svnadmin”

 

def create

svncmd “checkout”, resource[:name], resource[:path]

end

 

def destroy

FileUtils.rm_rf resource[:path]

end

 

def exists?

File.directory? resource[:path]

end

end

In the above code, we have upfront defined that we need fileutils library, require ‘fileutils’ which we are going to use method from.

Next, we have defined the provider as block Puppet::Type.type(:repo).provide(:svn) do which tells Puppet that this is the provider for type called repo.

Then, we have added desc which allows to add some documentation to the provider. We have also defined the command that this provider will use. In the next line, we are checking the features of resource like create, delete, and exist.

Creating a Resource

Once all the above is done, we will create a resource which will be used in our classes and manifest files as shown in the following code.

repo { “wp”:

source => “http://g01063908.git.brcl.org/trunk/”,

path => “/var/www/wp”,

ensure => present,

}

So, this brings us to the end of blog. This Tecklearn ‘Concept of Type and Provider 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 "Concept of Type and Provider in Puppet"

Leave a Message

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