Hello Puppet Users,
The following is the result of my first attempt to create a useful
component definition of something I wanted Puppet to manage. Instead of
just copying over "precompiled" syslog.conf files, I thought it would
be
more useful if I could specify the elements of the syslog.conf file.
(Ultimately, I think this should be defined as a type.)
I hit a few hurdles and I got around them in rather hackish ways, I think.
First, I knew it would be too much trouble to try to figure out how to
manage semicolon separated lists of selectors. So, I decided that for now,
I was okay with not having such lists. Ergo, I have an exec that detects
and cleans those.
Second, I knew grep and sed would freak out if there were *''s and
.''s in
the selector or the action. So, I created the definition such that one
could explicitly define the regex needed to properly match the respective
elements.
So, as an example, here is what my syslog component statement looks like in
use:
syslogconf {
"*.debug":
ensure => "/var/log/messages",
selector_regex => ''^\*.debug[[:space:]]'';
"*.err":
ensure => "/dev/console",
selector_regex => ''^\*.err[[:space:]]'';
"*.emerg":
ensure => "*",
selector_regex => ''^\*.emerg[[:space:]]'',
action_regex => "\*";
"lpr.err":
ensure => "/var/log/messages";
"lpr.crit":
ensure => "/dev/console";
}
Here is the syslog class definition:
class syslog {
Exec { path => "/usr/bin:/usr/sbin/:/bin:/sbin" }
$file = "/etc/syslog.conf"
file { "/etc/syslog.conf":
owner => root,
group => root,
mode => 644,
}
# Clean the syslog.conf file of any semicoloned selectors
exec { "cleansysconfig":
command => "sed -i -e ''/^[^#].*;/d''
$file",
onlyif => "grep ''^[^#].*;'' $file",
}
service { syslog:
name => $operatingsystem ? {
debian => sysklogd,
redhat => syslog
},
ensure => running
}
}
And here is the component definition:
# Example of how to set an syslogconf
# syslogconf { "*.err": ensure => "/var/log/messages"
}
#
# Example of how to ensure there is no syslogconf
# syslogconf { "*.err": ensure => absent }
#
# If you are using any special characters in the selector or action, as
defined
# in the syslog.conf(5) man page, you will want to specify a selector_regex
# and/or an action_regex to help the grep and sed operations properly match
the values
#
# NOTE: This definition does not support the use of semicolons in
# the selector field to specify multiple priority specifications
define syslogconf(ensure, selector_regex="BASIC_REGEX",
action_regex="BASIC_REGEX") {
Exec { path => "/usr/bin:/usr/sbin/:/bin:/sbin" }
$file = "/etc/syslog.conf"
$selector_pattern = $selector_regex ? {
BASIC_REGEX => "^$name",
default => $selector_regex
}
$action_pattern = $action_regex ? {
BASIC_REGEX => "$ensure",
default => $action_regex
}
case $ensure {
absent: {
exec { "rm-syslogconf-$name":
command => "sed -i -e ''/^$name/d''
$file",
onlyif => "grep ''^[^#]'' $file | grep
$selector_pattern
$file",
notify => service["syslog"]
}
}
default: {
$line = "$name\t\t\t$ensure"
exec { "add-syslogconf-$name":
command => "echo ''$line'' >>
$file",
unless => "grep ''$selector_pattern''
$file",
notify => service["syslog"]
}
exec { "fix-syslogconf-$name":
command => "sed -i -e
''/$selector_pattern/d'' $file; echo
''$line'' >> $file",
unless => "grep
''$selector_pattern[[:space:]]*$action_pattern'' $file",
require => exec["add-syslogconf-$name"],
notify => service["syslog"]
}
}
}
}