我正在尝试在 Knife/Ruby 环境中执行 bash 脚本。例如:
cookbook_file "test.sh" do
path "/tmp/test.sh"
mode "755"
action :create
end
bash "execute test.sh on #{nodeName}" do
code <<-EOH
sh test.sh arg1 arg2
EOH
#only_if { false }
end
如何使用 only_if
或 not_if
?所以当我们第二次执行并且“test.sh”的内容没有改变时,它应该跳过执行。我得到这个:
* cookbook_file[test.sh] action create (up to date)
但它仍然执行第二次、第三次...
最佳答案
您不会使用守卫,而是使用通知:
execute 'run test' do
action :nothing
command 'bash /tmp/test.sh arg1 arg2'
end
cookbook_file "test.sh" do
path "/tmp/test.sh"
mode "755"
notifies :run, 'execute[run test]', :immediately
end
资源更新时会触发通知,因此每当说明书文件发生更改时,它都会运行execute
。此外,您还希望使用 execute
而不是 bash
或 script
,因为您运行的是命令而不是内联脚本文件。
关于ruby - 在ruby/knife环境中执行bash脚本时如何实现幂等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026936/