Chef-在其他资源失败时运行资源

Jah*_*yst 5 ruby chef-infra chef-recipe

我有两个execute资源称为command_1command_2

如果command_1失败,我要运行command_2然后重新运行command_1

几乎是这样的:

execute 'command_1' do
  command "ipa host-del #{machine_name}"
  action :run
  ignore_failure true
  on_failure { notifies :run, 'execute['command_2']', :immediately }
end

execute 'command_2' do
  command "ipa host-mod --certificate  #{machine_name}"
  action :nothing
  notifies :run, 'execute['command_1']', :immediately
end
Run Code Online (Sandbox Code Playgroud)

我该如何用实际可行的方法代替on_failure(如果Chef拥有这个,那会很棒)?

Tej*_*don 3

最好的选择是首先放置 command_2,然后通过检查它是否需要运行来保护它。我不确定该命令的作用,但如果可以以某种方式验证是否需要它,那么您可以这样做。

如果无法验证是否需要 command_2,那么您可以这样做:

execute 'command_1' do
  command "ipa host-del #{machine_name} && touch /tmp/successful"
  action :run
  ignore_failure true
end

execute 'command_2' do
  command "ipa host-mod --certificate  #{machine_name}"
  action :run
  notifies :run, 'execute['command_1']', :immediately
  not_if { ::File.exist? '/tmp/successful' }
end

file '/tmp/successful' do
  action :delete
end
Run Code Online (Sandbox Code Playgroud)

这将运行 command_1,如果成功,它将运行touch /tmp/successful。然后,command_2 将运行,但前提是/tmp/successful不存在。如果command_2确实运行,它将通知command_1立即再次运行。为了清理下一次运行,我们添加file资源以确保/tmp/successful被删除