Chef - 运行 其他资源失败的资源

Chef - Run resource on other resource's failure

我有两个 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

我如何用实际有效的东西替换 on_failure(如果 Chef 有这个就好了)?

你最好的办法是先放 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

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