- From: Alexander Skwar <
>
- To:
- Subject: [chef] FileEdit - search_file_replace_line - Replace with content found
- Date: Thu, 27 Aug 2015 14:34:41 +0200
Hello
We are using chef-solo 11.8.2 and in a recipe, I'd like to use
Chef::Util::FileEdit.search_file_replace_line to replace a line
matching some regexp and "capture" what has been found and use this in
the replacement.
How? :)
Now, in a recipe, we have got the following:
ruby_block "CIS: AutoFS start deaktivieren" do
block do
fe = Chef::Util::FileEdit.new("/etc/init/autofs.conf")
# "start on" auskommentieren
fe.search_file_replace_line(/^(start on.*)/,
"# removed because of CIS check:\n# \1"
)
# Datei schreiben
fe.write_file
end # of block do
end # of ruby_block "CIS: AutoFS start deaktivieren" do
This is supposed to comment any lines starting with "start on" in the
autofs.conf file. It should replace the lines with the following
"block":
# removed because of CIS check:
# start on...
With perl, I used to have this:
perl -pi -e 's/^(start on.*)/# removed because of CIS check:\n# $1/'
/etc/autofs.conf
This works.
But this does not:
fe.search_file_replace_line(/^(start on.*)/,
"# removed because of CIS check:\n# \1"
)
It has replaced the "start on" lines just fine, but it added a \1...
It should have replaced the \1 by what's in the first "caputre"
(parentheses; ie. what's in "(" .. ")"; ie. "start on" and whatever is
following it).
How to do that?
Thanks a lot,
Alexander
--
=> Google+ =>
http://plus.skwar.me <==
=> Chat (Jabber/Google Talk) =>
<==
- [chef] FileEdit - search_file_replace_line - Replace with content found, Alexander Skwar, 08/27/2015
Archive powered by MHonArc 2.6.16.