How to unzip tar.Z file through CHEF cookbooks
Asked Answered
I

4

9

I'm using a Chef hosted server, a workstation and nodes and have run cookbooks on the nodes to install Java, update hosts file. I'm not able to find a reference in sites to unzip tar files. Could you please help me out here or direct to some site which has the information.

Thanks in advance.

Istic answered 10/2, 2015 at 21:15 Comment(0)
F
16

There's actually nothing built into Chef for doing extracting a tar file. You have two options, either you can use the execute resource to shell out and untar or use some community cookbook like the tar cookbook that have custom resources defined for extracting tars.

In the execute resource example it might look something like

execute 'extract_some_tar' do
  command 'tar xzvf somefile.tar.gz'
  cwd '/directory/of/tar/here'
  not_if { File.exists?("/file/contained/in/tar/here") }
end

Whereas the third party tar cookbook definitely reads nicer

tar_package 'http://pgfoundry.org/frs/download.php/1446/pgpool-3.4.1.tar.gz' do
  prefix '/usr/local'
  creates '/usr/local/bin/pgpool'
end
Fleischer answered 11/2, 2015 at 3:50 Comment(1)
The tar cookbook isn't actively under development any more. And I cannot get it to work with local extraction so the variant with execution of the tar command was working for me. There are alternative cookbooks (like tarball-chef-cookbook but that isn't part of the supermarket and does not support custom tar parameters.Bemire
S
3

Since Chef Client 15.0 there is a archive_file resource built-in. It supports tar, gzip, bzip, and zip.

archive_file 'Precompiled.zip' do
  path '/tmp/Precompiled.zip'
  destination '/srv/files'
end
Square answered 12/6, 2020 at 9:7 Comment(0)
D
0

Firstly the package 'tar' needs to be installed and then we can use chef's execute resource to run the tar command. You could use the following snippet.

package 'tar'
execute "extract tar" do
 command 'tar -xf #{tarPath} -C #{installPath}'
end 
Disproportionate answered 10/6, 2021 at 15:32 Comment(0)
C
0

Below chef resource works absolutely fine

execute "Extract tar file" do
  command "tar -xzvf #{Chef::Config['file_cache_path']}/temp.tgz -C #{Chef::Config['file_cache_path']}/temp"
  action :run
end

'temp.tgz' archive file will get extracted into 'temp' dir

path for '#{Chef::Config['file_cache_path']}' would be like /root/.chef/local-mode-cache/cache/ on linux distributions.

Coldiron answered 16/3, 2022 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.