aboutsummaryrefslogtreecommitdiff
path: root/manifests/defines/line.pp
blob: 7ca3191b864a5588f10246aea24d9e84187587ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# common/manifests/defines/line.pp -- a trivial mechanism to ensure a line exists in a file
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.

# Usage:
# line { description:
#   file => "filename",
#   line => "content",
#   ensure => {absent,*present*}
# }
#
# Example:
# The following ensures that the line "allow ^$munin_host$" exists
# in /etc/munin/munin-node.conf, and if there are any changes notify the service for
# a restart
#
# line { allow_munin_host:
#       file => "/etc/munin/munin-node.conf",
#       line => "allow ^$munin_host$",
#       ensure => present,
#       notify => Service[munin-node],
#       require => Package[munin-node],
# }
#
#
define line($file, $line, $ensure = 'present') {
  case $ensure {
    default : { err ( "unknown ensure value '${ensure}'" ) }
    present: {
      exec { "echo '${line}' >> '${file}'":
        unless => "grep -qFx '${line}' '${file}'"
      }
    }
    absent: {
      $subst_line = regsubst($line,'(/|\.)','\\\1','G')
      exec { "sed -i '/${subst_line}/d' '${file}'":
        onlyif => "grep -qFx '${line}' '${file}'"
      }
    }
  }
}