Automate Installation of Perl and Sudo packages with Puppet
Requirement
I need to install 2 packages, perl and sudo, and also want to re-install if these go missing. Try to do this manually and you could realize the manual effort required. But the good news is you could completely automate this using Puppet.
You could write a Puppet module for installing the packages on RHEL 6.4 and Ubuntu.
You could write a Puppet module for installing the packages on RHEL 6.4 and Ubuntu.
Pre-requisite
Puppet server
Puppet client installed (You could find this article in my blog)
RHEL 6.x
Ubuntu 14.x
Configure yum and apt-get repositories, very important.
Puppet client installed (You could find this article in my blog)
RHEL 6.x
Ubuntu 14.x
Configure yum and apt-get repositories, very important.
Setup DNS or simpley put your hosts in /etc/hosts file on all servers
Hosts
I have 2 client machines, one running RHEL 6.4 and other on Ubuntu 14. I already configured these as puppet clients to the puppet server and already signed their SSL certificates on the puppet server.
1. web
2. db
You could configure you own hosts, in any numbers you need.
1. web
2. db
You could configure you own hosts, in any numbers you need.
Action! Create puppet module
Directory structure for module
files (this folder holds any files we want to distribute)
templates (contains any template which our module will use)
manifests (will hold init.pp)
# mkdir -p /etc/puppet/modules/perl_sudo/{files,templates,manifests}
Create init.pp file
Every puppet module must have a init.pp file, this file is the core of a module.
# vi /etc/puppet/modules/perl_sudo/manifests/init.pp
class perl_sudo {
package { perl:
ensure => present,
}
package { sudo:
ensure => present,
}
if $operatingsystem == "Ubuntu" {
package { "sudo-ldap":
ensure => present,
require => Package["sudo"],
}
}
file { "/etc/sudoers":
owner => "root",
group => "root",
mode => 0440,
source => "puppet://$puppetserver/modules/sudo/etc/sudoers",
require => package["sudo"],
}
}
Above is a single class having 3 resources:
- The class perl_sudo. Ensures that package perl is installed, on both RHEL and Ubuntu clients
- Three packages, perl, sudo and sudo-ldap
- A file resource, sudoers
The $operatingsystem variable is checked and if target client is running Ubuntu then sudo-ldap package is installed on it. For your knowledge, the $operatingsystem variable comes from the package facter as a fact made available to your puppet module.
The sudoers file resource will look for the sudoers file at below location, you need to place a copy of the sudoers file:
# mkdir –p /etc/puppet/modules/sudo/files/etc
# cp /etc/sudoers /etc/puppet/manifests/files/etc/sudoers
Thats it! your first puppet module is ready!
When you apply this module, it will do the following:
1. Installs packages, sudo and perl.
2. If it's an Ubuntu client, it will install sudo-ldap package
3. Installs sudoers file into /etc
Apply this module on puppet clients
# vi /etc/puppet/manifests/nodes.pp
node 'web' {
include perl-sudo
}
node 'db' {
include perl_sudo
package { 'vim-minimal': ensure => present }
}
You could notice I have put package vim-minimal in nodes.pp. You bet, this will get installed automatically on your puppet clients. Good place, you could try to install other packages here, and not only site.pp file.
Apply the module now
# puppet agent --server=pserver.private.cluster1.example.com --no-daemonize --verbose --onetime
You should get messages like below:
[root@db etc]# puppet agent --no-daemonize --verbose --onetime
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for db
Info: Applying configuration version '1429015853'
Notice: Finished catalog run in 0.30 secondsVerify the packages, perl, sudo and vim got installed?
[root@db etc]# rpm -q perl sudo vim-minimal
perl-5.10.1-131.el6_4.x86_64
sudo-1.8.6p3-7.el6.x86_64
vim-minimal-7.2.411-1.8.el6.x86_64
perl-5.10.1-131.el6_4.x86_64
sudo-1.8.6p3-7.el6.x86_64
vim-minimal-7.2.411-1.8.el6.x86_64
Here's my screenshot with the error I got and how I resolved it:
The error was that package vim could not be installed. Why?
Because there is no package named 'vim'. The correct package name was 'vim-minimal'. I corrected this in the module in nodes.pp and bingo my module worked completely fine.
I re-applied the module from client machine 'db' and got vim-minimal package installed:
You've got them all! and do the same in the Ubuntu client also, be sure to configure your repositories.
Hint: You could re-write this module to distribute your /etc/hosts file :) Tell me if you do this.
Cheers! See you next time.



ReplyDeleteExcellent idea you have shared. Thank you for the useful info. Share more updates.
Azure Online Training
microsoft azure online training