ruby - 在ruby/knife环境中执行bash脚本时如何实现幂等性?

标签 ruby chef-infra knife

我正在尝试在 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_ifnot_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 而不是 bashscript,因为您运行的是命令而不是内联脚本文件。

关于ruby - 在ruby/knife环境中执行bash脚本时如何实现幂等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026936/

相关文章:

ruby-on-rails - 订单完成后重定向回 "purchases"页面

ruby - 在 Windows 上安装 Chef 服务器

ruby - Chef-Ruby-ArgumentsError预期为0

windows - Chef-Client 尝试在 Windows 上安装 Java,但它已经安装

ruby-on-rails - Ruby 正则表达式匹配双 mustache 但不匹配三 mustache

ruby - 如何在 Ruby 中通过 HTTPs 进行基本身份验证?

ruby - Chef/Knife ( ruby )错误 : superclass mismatch for class Edit (TypeError)

ruby - Chef knife 命令不显示 ssl 子命令

Ruby 使用来自 "namespace"的类

windows - 如何从 Windows 批处理文件中使用 Chef knife 实用程序?