如果不存在所需的安装包,如何告诉Chef采取操作?

Mah*_*der 3 apt chef-infra chef-recipe

我正在努力让厨师执行以下操作:

  • 检查是否有所需的debian(可以是任何包)
  • 如果是,apt-get安装包
  • 如果不是,请使用源构建包

我知道你可以这样做:

remote_file "some remote file" do
  ...
  not_if "apt-cache search 'mypackage'"
end
Run Code Online (Sandbox Code Playgroud)

但是,我尝试过:

ruby_block "Attempting to install #{node[:bact][:application_name]}" do
  block do
    cmd = Chef::ShellOut.new("apt-get install -y --force-yes #{node[:bact][:application_name]}")
    exec_result = cmd.run_command
    if exec_result.exitstatus != 0
      Chef::Log.info 'Go grab some coffee, this might be a while....'
      resources("execute[install-#{node[:bact][:application_name]}-via-pip]").run_action(:run)
    end
  end
  action :create
end
Run Code Online (Sandbox Code Playgroud)

有没有更简单,更简单的方法来做到这一点?

基本上,我理想的做法是:

begin
   package 'some-package-name' do
     action :install
   done
rescue Chef::Exception
   # Do something here
end
Run Code Online (Sandbox Code Playgroud)

Hol*_*ust 8

您可以使用ignore_failure true安装Debian软件包.然后,只有在此时未安装Debian软件包时才能安装pip软件包.这看起来像这样:

package node[:bact][:application_name] do
  ignore_failure true
end

# Resource available from the opscode python cookbook
python_pip node[:bact][:application_name] do
  # Install the pip package only if the debian package is not installed
  not_if "dpkg-query -W '#{node[:bact][:application_name]}'"
end
Run Code Online (Sandbox Code Playgroud)