Puppet syntax for if condition, variable, selector and class

Puppet Site:https://puppet.com/

Puppet Forge :https://forge.puppet.com/

git and puppet https://puppet.com/blog/how-to-use-git-commit-hooks-puppet-enterprise

Puppet variables=A variable hold the value for e.g.  x=1 where x is a variable which holds value 1.

Example that displays how to create and use variable in puppet

class linux{

    $admintools=['git','nano','othersoftware']
    
   package { $admintools:
      ensure=>'installed',
   }
}

 Puppet selectors =A selector assigns one of a set of possible values to a defined variable based on the condition
$ntpservice= variable
$osfamily = one of the fact from puppet

$ntpservice=$osfamily ? {
     'redhat' => 'ntpd',
     'debian' => 'ntp',
     'default' => 'ntp',
}

How to use it?

Replace 
service {'ntpd':
       ensure=>'running',
       enable => true,
}
With
service{$ntpservice:
       ensure => 'running',
       enable =>  true,
}

Class in puppet != Object Oriented Programming (oop) class  in puppet class represent  a named collection of resources declaration such as variable, selector or any other puppet code. It is simply a code container. Class does help to maintain DRY(Don’t repeat yourself principle).

How to create a class in puppet?

class linux{
   package{ 'ntp':
   ensure=> 'installed',
   }
}

How to use/reuse a class?

node 'wiki'{ {class 'linux':} }

node 'wikiexample'{ {class 'linux':} }

Use of If condition in puppet

//checking a condition if osfamily is redhat then install package otherwise skip the block of code
if $osfamily=='redhat'{
  package{'php-xml':
       ensure=>'present',
  }
}

A complete example showing  if condition, variable, selector and class usages in a file init.pp (,pp is the extension for the puppet file)

//define this code under init.pp
class mediawiki{

   $phpmysql=$osfamily ? {
    'redhat' => 'php-mysql'.
    'debian' => 'php5-mysql',
    default  =>  'php-mysql',
  }
  
  package{$phpmysql:
   ensure=>'present',
 }
if $osfamily=='redhat'{
    package{'php-xml':
    ensure=>'present', 
   }
 }
}
//end of code for init.pp

//create nodes.pp file

node 'wiki'{
   class {'mediawiki':}
}

node 'wikiTest'{
   class {'mediawiki':}
}

node 'wikiStage'{
   class {'mediawiki':}
}

node 'wikiProd'{
   class {'mediawiki':}
}
RESOURCE_TYPE { TITLE:
  ATTRIBUTE => VALUE,
  ...
}

Puppet Commands:

List all certificates

puppet cert list --all  # This code will show list of certificates 
puppet cert sing -a  #Sign all certificate