How to move / copy files locally with a chef

I have not yet come across a Chef resource that will copy / move files locally. For example, I want to load a hightide pier and unzip it. After that, copy all the files to a specific folder, for example:

# mv /var/tmp/jetty-hightide-7.4.5.v20110725/* /opt/jetty/ 

BTW, jettyhightide when unpacking, gives you a folder and the rest of the files are inside this folder. Therefore, unzip jetty-hightide-7.4.5.v20110725.zip -d /opt/jetty/ useless because it will create the /opt/jetty/jetty-hightide-7.4.5.v20110725/* directory, whereas I really want /opt/jetty/* . Therefore, I am looking for a local copy / move resource in Chef.

+44
chef
07 Oct '13 at 9:30
source share
8 answers

I worked using the bash resource as shown below:

 bash "install_jettyhightide" do code <<-EOL unzip /var/tmp/jetty-hightide-7.4.5.v20110725.zip -d /opt/jetty/ mv /opt/jetty/jetty-hightide-7.4.5.v20110725/* /opt/jetty/ cp /opt/jetty/bin/jetty.sh /etc/init.d/jetty update-rc.d jetty defaults EOL end 

But I really hoped it would be a chef. copying / moving files locally would be the most common task that sysadmin has to do.

+13
07 Oct '13 at
source share

How to copy a single file

First way

I use the file operator to copy a file (compile time check)

 file "/etc/init.d/someService" do owner 'root' group 'root' mode 0755 content ::File.open("/home/someService").read action :create end 

here:

  • "/etc/init.d/someService" - the target file,
  • "/home/someService" - source file

You can also wrap ::File.open("/home/someService").read in lazy block

 ... lazy { ::File.open("/home/someService").read } ... 

Second way

remote_file user (runtime check)

 remote_file "Copy service file" do path "/etc/init.d/someService" source "file:///home/someService" owner 'root' group 'root' mode 0755 end 

Third way

You can also use shell / batch

For each directory

 Dir[ "/some/directory/resources/**/*" ].each do |curr_path| file "/some/target/dir/#{Pathname.new(curr_path).basename}" do owner 'root' group 'root' mode 0755 content lazy { IO.read(curr_path, mode: 'rb').read } action :create end if File.file?(curr_path) directory "/some/target/dir/#{File.dirname(curr_path)}" do path curr_path owner 'root' group 'root' mode 0755 action :create end if File.directory?(curr_path) end 

This is just an idea because the subparagraphs in this example are not processed correctly.

+92
31 Oct '13 at 12:45
source share

I know that this question has already been answered and discussed, but here is the method that I use when creating files.

  • First include the file under the cookbook / default folder
  • Then in your recipe use the cookbook_file resource

eg:

 cookbook_file "/server/path/to/file.ext" do source "filename.ext" owner "root" group "root" mode 00600 action :create_if_missing end 

From the chef's documentation: http : //docs.opscodecode/resource_cookbook_file.html

The cookbook_file resource is used to transfer files from a subdirectory of a file / directory in a cookbook with the specified path, which is located on the host where the chef or chef works.

+13
Feb 17 '14 at 21:45
source share

You can try ark cookbook. This extracts the file for you, and after that you notice the execute resource.

+9
Oct 07 '13 at 17:32
source share

In addition to how you did this and accepted, if you only wanted to run one command, as you originally asked (copy or move), and not run the command block, you can do this using the execute resource:

 execute "copy_core" do command "mv /var/tmp/jetty-hightide-7.4.5.v20110725 /opt/jetty" user "root" end 

Perhaps this will help someone else to look at this in the future.

+2
Jul 23 '15 at 10:42
source share

I would use something like the following (note the β€œbinread”), as this will work for text files and binary files. using "read" will give amazing results with binary files, especially if you use both unix and windows systems.

 file destination do content IO.binread(source) action :create end 
+1
Oct 20 '16 at 6:50
source share

Copy files locally to CHEF

 file "C:/Users/Administrator/chef/1.xml" do ---> tar content lazy { IO.read("C:/Users/Administrator/chef-repo/cookbooks/2.xml") } -->src action :create end 
0
Jul 21 '15 at 10:23
source share

If your recipe is already tied to Windows, you can use PowerShell built-in scripts, for example:

 # Copy files from "C:/foo/lib" to "C:/foo" powershell_script "copy_lib" do code <<-EOH $ErrorActionPreference = "Stop" Get-ChildItem -Path "C:/foo/lib" -File | Foreach-Object { Copy-Item -Path $_.Fullname -Destination "C:/foo" -Force } EOH end # Delete "C:/foo/lib" folder powershell_script "delete_lib" do code <<-EOH $ErrorActionPreference = "Stop" Remove-Item -Path "C:/foo/lib" -Recurse EOH end 
0
Dec 08 '17 at 18:56 on
source share



All Articles