opscode chef可以执行wait直到一个条件变为真吗?



我们有一个用例,在这个用例中,我们希望chef编排等待,直到机器中的特定目录被删除。有什么方法可以实现吗?

我在网上搜索,找到了以下食谱

我觉得它可以使用,但我很难理解我该如何使用它,没有关于使用它的阅读我。

我怎样才能实现它?

edit to remove hold:假设你有以下食谱

execute 'making dir' do
  command 'mkdir /tmp/test2'
  not_if do ::File.directory?('/tmp/test1') end
end

参考:https://docs.chef.io/resource_common.html not-if-examples

这里,我想通过not_if是"等待,直到/tmp/test1被删除",但chef如何执行这是像"它发现目录存在,所以它没有执行资源并退出"

我需要一种方法来执行wait直到一个条件为真

这实际上是我在各种烹饪书中不时看到的一种模式,通常用于等待块设备或挂载某些东西,或者等待云资源。对于您找到的等待食谱,我不得不在Github上找到实际的源代码仓库来弄清楚如何使用它。下面是一个例子:

until 'pigs fly' do
  command '/bin/false'
  wait_interval 5
  message 'sleeping for 5 seconds and retrying'
  action :run
end

它似乎调用ruby的system(command)sleep(wait_interval)。希望这对你有帮助!

编辑:正如其他发帖者所说,如果你可以在chef中完成所有的事情,一个带有目录资源和删除操作的通知是一个更好的解决方案。但是你问如何使用等待资源,所以我想专门回答这个问题。

首先,不要浪费时间创建目录。如果您只使用Chef来执行shell命令,那么除了编写shell脚本之外,您不会获得更多的好处。依靠Chef目录资源为您完成这些工作要好得多。然后,您可以确信它在每个系统上每次都能正常工作。另外,您将能够使用代表您的目录的Chef资源,以便您可以执行诸如通知之类的操作。

下面是两个目录操作的稍微重构:

# Ensure that your directory gets deleted, if it exists.
directory '/tmp/test1' do
  action :delete
  notifies :action, 'directory[other_dir]', :immediately
end
# Define a resource for your directory, but don't actually do anything to the underlying machine for now.
directory 'other_dir' do
  action :nothing
  path '/tmp/test2'
end

相关内容

最新更新