How to optimize puppet class/module code ?

181 views
Skip to first unread message

ForumUser

unread,
Apr 9, 2013, 5:36:51 AM4/9/13
to puppet...@googlegroups.com
Hello all,

I am trying to write a syslog module for our small puppet installation.
Since I'd like to learn how to write puppet classes/modules I would avoid
modules from puppet labs forge (at least for now).

This module is going to be deployed on RH 5 _and_ 6.
In our infrastructure each RH line (5 or 6) is configured differently:
RH 5 uses sysklogd, RH 6 uses rsyslog.

I have written a class but as you can see it has a lot of redundant code.
Can I use any puppet syntax to make it more elegant (and easier to maintain :-) ) ?
Can you suggest anything ?

class syslog::install {
        case $lsbmajdistrelease {
                '5':    {
                                package { "sysklogd":
                                        ensure => present,
                                }
                        }

                '6':    {
                                package { "rsyslog":
                                        ensure => present,
                                }
                        }
        }
}

class syslog::config {
        case $lsbmajdistrelease {
                '5':    {
                                file    { "/etc/syslog.conf":
                                        ensure => present,
                                        owner => 'root',
                                        group => 'root',
                                        mode => 0644,
                                        source => "puppet:///modules/syslog/syslog.conf",
                                        require => Class["syslog::install"],
                                        notify => Class["syslog::service"],
                                }
                        }

                '6':    {
                                file    { "/etc/rsyslog.conf":
                                        ensure => present,
                                        owner => 'root',
                                        group => 'root',
                                        mode => 0644,
                                        source => "puppet:///modules/syslog/rsyslog.conf",
                                        require => Class["syslog::install"],
                                        notify => Class["syslog::service"],
                                }
                        }
        }
}

class syslog::service {
        case $lsbmajdistrelease {
                '5':    {
                                service { "syslog":
                                        ensure => runing,
                                        enable => true,
                                        require => Class["syslog::config"],
                                }
                        }

                '6':    {
                                service { "rsyslog":
                                        ensure => runing,
                                        enable => true,
                                        require => Class["syslog::config"],
                                }
                        }
        }
}


class syslog {
        include syslog::install, syslog::config, syslog::service
}


Thanks in advance :-)
Przemek

Gavin Williams

unread,
Apr 9, 2013, 5:47:40 AM4/9/13
to puppet...@googlegroups.com
Hi there,

I think you're quickest win for making the code cleaner and easier would be to create a ::params class, which sets the correct values for package, file and service based on your distro.

Can then inherit this class on your ::install, ::config and ::service classes.

I'm sure other people will chime in with some other ideas aswell :)

HTH

Gav

ForumUser

unread,
Apr 9, 2013, 5:54:27 AM4/9/13
to puppet...@googlegroups.com
Hi Gavin,

Can you suggest any URL where I could read about ::params classes (and examples of course ;-) ) ?

fatmcgav

unread,
Apr 9, 2013, 5:56:30 AM4/9/13
to puppet...@googlegroups.com


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/vJeR5M2-TS0/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Stefan Heijmans

unread,
Apr 9, 2013, 6:23:40 AM4/9/13
to puppet...@googlegroups.com

ForumUser

unread,
Apr 9, 2013, 7:51:44 AM4/9/13
to puppet...@googlegroups.com
I have looked at this example. But what is a difference between 'File' and 'file' keyword ?
Where I can read more about it ?

ForumUser

unread,
Apr 9, 2013, 9:16:04 AM4/9/13
to Puppet Users
Hi there,

after reading the code I have changed my puppet code to the following
(thanks ! - it is now much more readable :-) ).
But I don't know how to change the "source =>" from syslog::config
class to be universal.
Could you please help me ?

class syslog::params {
case $lsbmajdistrelease {
'5': {
$syslog_package_name = 'sysklogd'
$syslog_syslog_config = '/etc/
syslog.conf'
$syslog_service_name = 'syslog'
}

'6': {
$syslog_package_name = 'rsyslog'
$syslog_syslog_config = '/etc/
rsyslog.conf'
$syslog_service_name = 'rsyslog'
}
}
}

class syslog::install {
package { $syslog::params::syslog_package_name:
ensure => installed,
}
}

class syslog::config {
file { $syslog::params::syslog_config:
ensure => present,
owner => 'root',
group => 'root',
mode => 0644,
source => "puppet:///modules/syslog/syslog.conf",
require => Class["syslog::install"],
notify => Class["syslog::service"],
}
}

class syslog::service {
service { $syslog::params::syslog_service_name:
ensure => runing,
enable => true,
require => Class["syslog::config"],
}
}

class syslog {
include syslog::params, syslog::install, syslog::config,

Stefan Heijmans

unread,
Apr 10, 2013, 5:11:54 AM4/10/13
to puppet...@googlegroups.com
You could use sourceselect in source;
 
or use parameter in the source statement;
source => "puppet:///modules/syslog/syslog.conf_${::$lsbmajdistrelease}",
and create 2 syslog.conf files in the files directory;
syslog.conf_5
syslog.conf_6
 

Stefan Heijmans

unread,
Apr 10, 2013, 5:23:41 AM4/10/13
to puppet...@googlegroups.com
bad cp;
 
source => "puppet:///modules/syslog/syslog.conf_${::lsbmajdistrelease}",

jcbollinger

unread,
Apr 10, 2013, 10:07:12 AM4/10/13
to puppet...@googlegroups.com


On Tuesday, April 9, 2013 4:47:40 AM UTC-5, Gavin Williams wrote:
Hi there,

I think you're quickest win for making the code cleaner and easier would be to create a ::params class, which sets the correct values for package, file and service based on your distro.


A ::params class is a reasonable approach, but the quickest win would be to do the equivalent within the existing code, without creating a new class.  For example:

class syslog::install {
  $syslog_package = $lsbmajdistrelease ? {
    '5' => 'sysklogd',
    '6' => 'rsyslog'
  }

  package { ${syslog_package}:
    ensure => present
  }
}


Note also that even if the data ($syslog_package in this example) were pulled out to a separate ::params class, it would be undesirable to use class inheritance to ensure the data are initialized.  It is sufficient and preferable in this case to simply 'include' the ::params class at the beginning of the class using it.  Class inheritance would be needed only if the other classes were parameterized AND variables from the ::params class were used as parameter default values.


John

Reply all
Reply to author
Forward
0 new messages