简体   繁体   English

如何使用Chef食谱删除文件中的一行?

[英]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"). 我要删除行配置 (此单词“ configuration”位于文件“ 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

You do not add to or remove lines from files with chef. 您不会使用Chef添加或删除文件中的行。 Instead you replace the configuration file with yours, which you put into your cookbook under files/ or templates/ forlder. 取而代之的是,将配置文件替换为配置文件,然后将其放入食谱中的files/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. 当您仅在配置文件中添加/替换行时,您无法确定升级已安装的软件后您的配置文件是否正确。

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. 虽然我同意使用模板是首选且被认为是最佳做法,但有时您需要编辑可能存在变化且不适合模板模型的现有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM