Puppet

Install Oracle client using rsp file ,powerShell and puppet

Create a ps1 file for eg oracleclientInstall.ps1

#oracelclientInstall.ps1
cmd.exe
C:\MyTemp\Ora11gx32\setup.exe -responseFile "C:\MyTemp\Ora11gx32\response\runtime.rsp" -silent
exit

Calling powershell file via Puppet exec resource

#Installoracel.pp
exec { 'RegisterOracle':
  command   => file('C:\Temp\installoracleclientruntime.ps1'),
  provider  => powershell,
  logoutput => true,
}

puppet agent -t Installoracle.pp

exec{'oracle':
command  => "setup.exe -silent -responseFile E:/software/Win64_120102_client/client12c_64.rsp ",
path  => 'c:\apps',
 }

Run PS file using puppet exec command

in registerDLL.ps1 file

#registerDLL.ps1
$location=Set-Location -Path C:\Windows\SysWOW64
regsvr32.exe "C:\ProgramFiles(x86)_shiva\HashMgr\HashMgr.dll"

exec { 'RegisterDll':
  command   => file('C:\Temp\puppetcode\registerDLL.ps1'),
  provider  => powershell,
  logoutput => true,
}

Enable IIS AnonymousAuthentication using exec and puppet

exec{‘set-anon-auth’:
command => ‘Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/AnonymousAuthentication -name Enabled -Value True -location mysite’,
provider => ‘powershell’,
logoutput => true
}

Install exe file using puppet

package { “Access Database Engine Component”:
ensure => installed,
source => ‘C:\shivaTemp\AccessDatabaseEngine.exe’,
install_options => [ ‘/passive’, { ‘INSTALLDIR’ => ‘C:\shivaTemp’ } ],
}

$install_dir = ‘C:\shivaTemp’
package { “Access Database Engine Component”:
ensure => installed,
provider => ‘windows’,
source => ‘C:\shivaTemp\AccessDatabaseEngine.exe’,
install_options => [ ‘/passive’, { ‘INSTALLDIR’ => $install_dir } ],

}

puppet code for windows environment?

Building blocks of Puppet

  1. Resources
  2. Classes
  3. Manifest
  4. Modules

Install Puppet Windows Module Pack

puppet module install puppetlabs/windows

puppet module list

How to copy directory and it’s files using puppet

$soure_dir=c:\temp\puppetcode\Source
$destination_dir= c:\temp\puppetcode\Destination

file { $destination_dir :
ensure  => 'directory',
source  => "file://${source_dir}",
recurse => true,
}

Copy folder and remove the original folder

$soure_dir=c:\temp\puppetcode\Source
$destination_dir= c:\temp\puppetcode\Destination

file { $destination_dir :
ensure  => 'directory',
source  => "file://${source_dir}",
recurse => true,
before=> File[$source_dir],
}

file{$source_dir :
	ensure=>'absent',
	purge=>true,
	recurse=>,
	force=>true,

}

Simple code to copy folders

file {'/my/path':
    ensure  => 'directory',
    path    => '/my/path',
    recurse => true,
    source  => '/home/user_name/scripts',
    }

Running puppet code on different environment

puppet agent -t --environment production
puppet agent -t --environment stage
puppet agent -t --environment development

Adding git and auto deploy puppet code

git init --bare /srv/git/repos/mypuppetcode.git
git clone  /srv/git/repos/mypuppetcode.git

Add environment.conf file

moudulepath=site:modules:$basemodulepath
mainfest=mainfests/site.pp

Add working environment

puppet config print environment #Display the current environment

sudo puppet config set environment set environment dev

Create Modules

cd / etc/puppetlabs/code/environments/production/modules

sudo mkdir -p motd/{manifests,files,examples}

sudo vim motd/examples/init.pp

motd/mainfest/init.pp

class motd{
       file{'/etc/motd':
       ensure=> 'present',
       content=>file('motd/message'),
      }
}

sudo vim motd/files/message

write a content such as “Welcome to my server”

<environment> /modules

<modulename>/mainfests/init.pp

class motd{

}

content=>file(‘motd/message’) # motd/files/message

Modules Metadata

cd /etc/puppetlabs/code/environments/production/modules

sudo puppet module generate shiva/test

sudo puppet module generate shiva/test –skip-interview # skip

Create NTP module

/etc/puppetlabs/code/enviroments/production/modules

sudo mkdir -p ntp / {manifests,files,examples}

in windows

puppet module generate modules/ntp

Add puppet ACL for IIS Default App Pool

acl { 'C:\inetpub\wwwroot\uploads':
  permissions => [
   { identity => 'IIS AppPool\DefaultAppPool', rights => ['full'] }
 ],
}

Adding Registry Key Value

registry_key { 'hklm\software\mykey':
  ensure => present,
}

registry_value { 'hklm\software\mykey\value1':
  type => string,
  data => 'this is a value'
}

Configure Firewall

firewall-cmd --permanent --zone=public --add-port=8140/tcp
firewall-cmd –reload

Start/Enable puppet server

systemctl start puppetserver
systemctl enable puppetserver
systemctl status puppetserver
puppet status
netstat -anpl | grep 8140

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