Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to rename a file with chef

How can I rename a file with chef?

In the chef doc I found only:

  • create
  • create_if_missing
  • delete
  • touch
like image 347
Rusty Robot Avatar asked Nov 23 '12 11:11

Rusty Robot


People also ask

How do I rename a file in chef?

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.

How do you rename a file easily?

Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.

What is Cookbook_file?

Use the cookbook_file resource to transfer files from a sub-directory of COOKBOOK_NAME/files/ to a specified path located on a host that is running the chef-client.


2 Answers

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
like image 45
kamal079 Avatar answered Oct 08 '22 17:10

kamal079


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
like image 104
Sacx Avatar answered Oct 08 '22 15:10

Sacx