chef 运行 资源仅当存在包时

chef running resource only when a package is present

我想解压厨师食谱中的一个文件,但我想确保解压包在我解压之前安装在该机器上。如何在厨师中做到这一点?

我尝试了下面的方法,但是在安装了解压缩包的机器上根本不执行它。

bash 'extract' do
    cwd '/home/norun/'
    code <<-EOH
        unzip wso2.zip
        EOH
    only_if { ::File.exists?('/home/norun/wso2.zip') }
    not_if { ::File.exists?('/home/norun/wso2as-5.2.1') }
end

apt_package 'unzip' do
    action :install
    notifies :run , 'bash[extract]', :immediately
end

提前致谢

只需更改顺序,因为厨师的顺序很重要:

package 'unzip' do
    action :install
    # notifies :run , 'bash[extract]', :immediately
end

这将确保 unzip 实用程序已安装。顺便说一句,您可以使用 package 资源,因为这会使您的代码更具可移植性。 Chef 会自动 select 正确的供应商。此外,我建议从 package[unzip] 中删除通知,因为如果 unzip 已经安装,则不会触发解压缩过程。

bash 'extract_wso2' do
    cwd '/home/norun/'
    code 'unzip wso2.zip'
    only_if { ::File.exists?('/home/norun/wso2.zip') }
    not_if { ::File.exists?('/home/norun/wso2as-5.2.1') }
end

您不需要通知 bash[extract_wso2] 资源,因为它由 not_if.

保护

除了手动解压缩,您可能还想使用 ark 食谱。