Thursday, August 13, 2015

Puppet - Configuration Management

Introduction:

Puppet is an open-source IT automation tool. The Puppet Domain Specific Language (DSL) (gives you an operating-system-independent language) is a Ruby-based coding language that provides a precise and adaptable way to describe a desired state for each machine in your infrastructure. Once you've described a desired state, Puppet does the work to bring your systems in line and keep them there.

puppet resource user root /*The puppet resource command should be used to inspect user root*/

puppet describe user | less  */Man pages to understand the different attributes and value for defining a manifest.*/

Some of the type of resources are

 (->)User
 (->)File
 (->)Package
 (->)Service
 (->)Cron
 (->)Group


Resource Abstraction Layer or RAL */Puppet will remove the complexity of managing the resources across different operating systems. You can sit back and watch whether puppet uses apt, yum, rpm*/

puppet apply -e "user" { 'galatea': ensure => 'present', } /*puppet one-liner.The user galatea should exist on the system*/
Syntax:
        'resource'{ 'title':
                      attribute => 'value',
                  }


puppet resource -e user galatea /*Edit the resource user manifest. The user galatea should have the comment Galatea of Cyprus*/


Classes and Manifests

-Manifests:
  •  A manifest is a text file that contains puppet code. And is appended by .pp
  •  It's the same stuff you see using the `puppet resource tool` and applied with the `puppet apply`
  •  In theory, you could put whatever bits and pieces of syntactically valid Puppet code you like into a manifest.
  •  A key aspect of proper manifest management is related to Puppet classes.

-Classes:
  •  In puppet's DSL a class is a named block of Puppet code.
  •  A class will generally manage a set of resources related to a single function or system component
  •  Class often contain other classes
  •  Using a Puppet class requires two steps. First, you'll need to define it by writing a class definition and saving it in a manifest file. When Puppet runs, it will parse this manifest and store your class definition. The class can then be declared to apply it to nodes in your infrastructure.
  •  Classes are Singleton, which means that a class can be declared once on a given node.In this sense, Puppet's classes are different than the kind of classes you may have encountered in Object Oriented programming, which are often instantiated multiple times.

Validation Tools: 

  • The `puppet parser` tool can be used to check the syntax of your manifest before doing `puppet apply`
  • puppet parser validate cowsayings/manifests/cowsay.pp
Before applying a puppet note on "SCOPE": Below is an example of sample class module (under the path /etc/puppetlabs/puppet/environment/production/modules/cowsayings/manifests/) called cowsay.pp.

class cowsayings::cowsay {
            package {'cowsay' :
                ensure => 'present',
            }
}
 
In the above class definition,

  •  "cowsaying::" this SCOPE syntax definition tells Puppet where to find that class; in this case, it's in the cowsayings module.
  • Usually the Puppet recognizes the "/etc/puppetlabs/puppet/environment/production/modules/cowsayings/manifest/init.pp" manifest to contain the "main" class for the module. Just like below example:
class cowsayings {
    include cowsayings::cowsay
    include cowsayings::fortune
}
 

Remove the already installed packages namely "fortune-mod" and "cowsay" using the one-liner below and install using the puppet apply of manifest /etc/puppetlabs/puppet/environment/production/modules/cowsayings/test/init.pp". In init.pp the class are declared.

# puppet apply -e "package { 'fortune-mod':ensure => 'absent',} package { 'cowsay':ensure => 'absent',}"
# puppet apply --verbose  /etc/puppetlabs/puppet/environments/production/modules/cowsayings/tests/init.pp