Best way to rename a file with chef

混江龙づ霸主 提交于 2019-12-08 18:44:53

问题


How can I rename a file with chef?

In the chef doc I found only:

  • create
  • create_if_missing
  • delete
  • touch

回答1:


Use ruby_block and inside use ::File.Rename(src,dst). Chef framework doesn't have file rename (or at least didn't had until 0.10.18).

Just an example:

ruby_block "Rename file" do
  block do
    ::File.rename(new_resource.src,new_resource.dst)
  end
end



回答2:


Another option if you need to rename multiple files. Checks one of the resource to know if it already ran.

ruby_block "Rename file" do
  block do
    ::Dir.glob("*/*.src").each {|i| File.rename(i, i.gsub(/(.*).src/,'\\1.dst'))};
  end
  not_if {File.exists?("new_resource.dst")}
end


来源:https://stackoverflow.com/questions/13528409/best-way-to-rename-a-file-with-chef

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!