- From: Jesse Campbell <
>
- To: chef <
>
- Subject: [chef] Re: Chef Recipe inserting a line before last line in fie
- Date: Mon, 10 Dec 2012 20:56:30 -0500
I did this by making a template with some relatively simple regex.
I have since just templated the whole file, but this should still work.
Recipe:
lines[0]['text'] = "full line to insert"
lines[0]['insertpoint'] = "file line to find in the file"
template "/etc/file_to_edit" do
source "insertline.erb"
mode 0600
owner "root"
group "root"
variables(
:oldfile => "/etc/file_to_edit",
:insertlines => lines
)
end
Contents of insertline.erb:
<% output = File.read(@oldfile) -%>
<% @insertlines.each do |line| -%>
<% unless (output.match(/^#{line['text']}$/i)) -%>
<% output = output.sub(/^(#{line['insertpoint']})$/,"\\1\n#{line['text']}")
-%>
<% end -%>
<% end -%>
<%= output -%>
On Sun, Dec 9, 2012 at 1:37 AM,
<
>
wrote:
>
Hello,
>
>
I am new to Chef and Ruby and trying to figure out the proper way to add a
>
line
>
to /etc/security/access.conf prior to the last line that denies everything
>
not
>
already defined.
>
>
Not sure yet whether the best way is to use a Chef command of a Ruby Block.
>
>
I do not see a way in Chef to insert a new line searched for if the line
>
does
>
not already exist in the file prior to another line in the file.
>
>
I thought this would work:
>
>
file.replace_line_if_no_match but that adds it after the last line in the
>
file.
>
In security, all the entries in access.conf that allow access have to be
>
prior
>
to the line that denies everything not already allowed.
>
>
Basically, I want to see if "+:mongod:cron crond\n" already exists. If it
>
does, do not do anything. However, if it does not exist, insert it prior
>
to "-
>
: ALL : ALL\n".
>
>
What is the best way in a Chef Recipe to do this?
>
>
Thank you.
>
>
----
>
Charles Keagle
>
>
425-417-3434
Archive powered by MHonArc 2.6.16.