summaryrefslogtreecommitdiff
path: root/manifests/defines/replace.pp
blob: ae4498ee56b71db252bd80a9f40b8e74519da768 (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
# common/manifests/defines/replace.pp -- replace a pattern in a file with a string
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.

# Usage:
#
# # replace the current port in /etc/munin/munin-node.conf
# # with a new, only disturbing the file when needed
# replace { set_munin_node_port:
# 	file => "/etc/munin/munin-node.conf",
# 	pattern => "^port (?!$port)[0-9]*",
# 	replacement => "port $port"
# }  
define replace($file, $pattern, $replacement) {
	$pattern_no_slashes = slash_escape($pattern)
	$replacement_no_slashes = slash_escape($replacement)
	exec { "replace_${pattern}_${file}":
		command => "/usr/bin/perl -pi -e 's/$pattern_no_slashes/$replacement_no_slashes/' '$file'",
		onlyif => "/usr/bin/perl -ne 'BEGIN { \$ret = 1; } \$ret = 0 if /$pattern_no_slashes/; END { exit \$ret; }' '$file'",
		alias => "exec_$name",
	}
}