[chef] Re: Re: Re: FileEdit - search_file_replace_line - Replace with content found


Chronological Thread 
  • From: Joshua Timberman < >
  • To:
  • Subject: [chef] Re: Re: Re: FileEdit - search_file_replace_line - Replace with content found
  • Date: Thu, 27 Aug 2015 09:03:20 -0600

You might want to take a look at the "line" cookbook. It includes resources for managing file content in place using the FileEdit methods.



On Thu, Aug 27, 2015 at 7:35 AM, Alexander Skwar < " target="_blank"> > wrote:
Hello Matt

Thanks a lot, replace works just fine :)

For the archive, I've now got:

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(/^(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


Or see http://pastebin.com/diff.php?i=BQusWv7h for a diff…

I had to use "\\1" instead of "\1", as I'm in a double quoted string
(-> "). I'm in a double quoted string, as I'd like \n to add a new
line.

Matt,
thanks! You helped me very much!

Cheers,
Alexander



2015-08-27 15:15 GMT+02:00 Matthew Moretti < "> >:
> I notice in poking through the source that Chef::Util::FileEdit#replace uses
> gsub!, while Chef::Util::FileEdit#replace_line doesn’t.
>
> gsub! will use your backreferences. Since your regexp starts with ^ and ends
> with .*, it will match the entire line anyways; perhaps try replace instead?
>
> Matt Moretti
>
>
> On Thu, Aug 27, 2015 at 8:34 AM, Alexander Skwar
> < "> > wrote:
>>
>> 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) => "> <==
>
>



--


Alexander
--
=>        Google+ => http://plus.skwar.me         <==
=> Chat (Jabber/Google Talk) => "> <==




Archive powered by MHonArc 2.6.16.

§