如何通过 CHEF 食谱解压 tar.Z 文件

How to unzip tar.Z file through CHEF cookbooks

我正在使用 Chef 托管服务器、工作站和节点,并在节点上安装 运行 说明书以安装 Java,更新主机文件。我无法在站点中找到解压缩 tar 文件的参考。能否请你帮我在这里或直接到一些有信息的网站。

提前致谢。

Chef 实际上并没有内置任何东西来提取 tar 文件。你有两个选择,要么你可以使用 execute resource to shell out and untar or use some community cookbook like the tar cookbook that have custom resources defined for extracting tars.

execute 资源示例中,它可能类似于

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

而第三方 tar 食谱绝对 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

从 Chef Client 15.0 开始,内置了 archive_file 资源。它支持 tar、gzip、bzip 和 zip。

archive_file 'Precompiled.zip' do
  path '/tmp/Precompiled.zip'
  destination '/srv/files'
end

首先需要安装包 'tar' 然后我们可以使用 chef 的执行资源来 运行 tar 命令。您可以使用以下代码段。

package 'tar'
execute "extract tar" do
 command 'tar -xf #{tarPath} -C #{installPath}'
end 

下面的 chef 资源绝对没问题

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' 存档文件将被提取到 'temp' 目录

'#{Chef::Config['file_cache_path']}' 的路径类似于 /root/.chef/local-mode-cache/cache/ 在 linux 发行版上。