Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a line in a file using Chef recipe?

I want to delete the line configuration (This word "configuration" is inside the angular brackets in the file "hdfs-site.xml"). I tried using this code but no luck.

ruby_block "delete_lines_in_hdfs_site" do
block do
  file = Chef::Util::FileEdit.new("/opt/hadoop-2.4.1/etc/hadoop/hdfs-site.xml")
  file.search_file_delete_line(/<configuration>/)
end
end
like image 554
Shesh Kumar Bhombore Avatar asked Nov 23 '25 17:11

Shesh Kumar Bhombore


2 Answers

You need to end the ruby block with

file.write_file

Otherwise the changes are not written out.

While I agree that using a template is preferred and considered best practices, there are times you need to edit an existing file which may have variations and not fit the template model.

like image 187
Roger Joys Avatar answered Nov 25 '25 10:11

Roger Joys


You do not add to or remove lines from files with chef. Instead you replace the configuration file with yours, which you put into your cookbook under files/ or templates/ forlder.

template "/opt/hadoop-2.4.1/etc/hadoop/hdfs-site.xml"

or

cookbook_file "/opt/hadoop-2.4.1/etc/hadoop/hdfs-site.xml"

When you just add/replace lines in config files, you cannot be sure, that after upgrade of installed software your config files are right.

like image 41
Draco Ater Avatar answered Nov 25 '25 09:11

Draco Ater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!