sed power on config files (etc/lvm/lvm.conf) with Puppet

ISSUE:

In the file /etc/lvm/lvm.conf the value of ‘level = x’ has to be set to 3 (lvm errors will be logged to syslog)

Further requirements:

  1. This only has to be done if the value is different (unless..)
  2. ‘level = x’ within comments (#) have to be omitted
  3. Only if the value has changed the lvm config needs to be reloaded (boot.lvm on Suse to be notified)
  4. This has to be included in a Puppet manifest

SOME SAMPLES ON HOW TO GET THERE

SERVER1:~ # sed -n ‘/level/p’ /etc/lvm/lvm.conf
# There are three levels of verbosity, 3 being the most verbose.
# What level of log messages should we send to the log file and/or syslog?
# There are 6 syslog-like log levels currently in use – 2 to 7 inclusive.
# 7 is the most verbose (LOG_DEBUG). level = 35
level = 3
# Set to 0 to decrease the log level of missing physical volume.

SERVER1:~ # sed ‘/^#/!s/test/TEST/g’ /etc/lvm/lvm.confSERVER1:~ # sed -n ‘/^#/!s/test/TEST/p’ /etc/lvm/lvm.conf
# When TESTing I like to work with just loopback devices:
# Enabling TEST mode means that no changes to the on disk metadata
TEST = 0
# The feature was originally added to simplify TESTing and is not

SERVER1:~ # sed -n ‘/^[[:space:]]*#/!s/level/TEST/p’ /etc/lvm/lvm.conf
TEST = 3

APPLY TO PUPPET MANIFEST
### Use to set ‘level = 3’ in /etc/lvm/lvm.conf

if $::osfamily == ‘Suse’ {
exec {
‘lvm_set_log_level_3’:
command  => ‘sed  “/^[[:space:]]*#/!s/\(level\) = .*/level = 3/” -i /etc/lvm/lvm.conf’,
unless   => ‘grep level /etc/lvm/lvm.conf | grep -v “^[[:space:]]*#” | grep -w “level = 3″‘,
notify      => Service[“boot.lvm”],
}
}

SOURCES
http://stackoverflow.com/questions/7598133/vim-global-search-replace-starting-from-the-cursor-position

>> http://stackoverflow.com/questions/20568515/how-to-use-sed-to-replace-a-config-files-variable

>> http://stackoverflow.com/questions/4798149/ignore-comments-using-sed-but-keep-the-lines-untouched

http://www.cyberciti.biz/faq/unix-linux-sed-print-only-matching-lines-command/