如何查看和使用 Chef 资源集合

How to see and use the Chef resource collection

我正在使用 Chef Supermarket 的 Jenkins Chef Cookbook。

它提供了一个jenkins_commandresource/provider。例如,这允许我重新加载 Jenkins 配置

jenkins_command 'reload-configuration'

这很有用,但我不想为每个 Chef 客户端重新加载配置 运行。

现在的问题是好像不能通知这个资源去执行

首先我用动作定义了资源:nothing

jenkins_command 'reload-configuration' do
  action :nothing
end

我添加了应该触发重新加载配置的资源

notifies :execute, 'jenkins_command[reload_configuration]', :delayed 

这会产生类似于

的消息

jenkins_command[reload_configuration] cannot be found in the resource collection

我不清楚为什么这不起作用。这是否意味着该资源是资源但未添加到资源集合中?

或者我是否应该使用不同的名称通知资源?

如何查看资源集合的外观?有没有办法查看它的一部分?

resources 个对象,但该对象始终为空 []。我可以检查使用 Chef::Log 或使用 log 资源,它总是空的。

检查资源集合的正确方法是什么? Chef 文档未提供任何信息 https://docs.chef.io/resource.html。它只说明我们应该能够使用任何资源。

首先,我认为您的问题是由于 -_ 引起的。您将资源命名为 reload-configuration 但您通知 reload_configuration.

其次,无论何时您尝试触摸资源集合,您都会陷入困境。您可以在 run_context.resource_collection 中找到它。使用风险自负。

resources 本身不是对象,它是 Chef::Recipe 中定义的方法调用:https://github.com/chef/chef/blob/f4a47f9e248d99fe6c284bcbff7c2b05d6dd0484/lib/chef/recipe.rb#L82

您可以使用该帮助程序或直接调用集合上的其他方法,但这种方式太疯狂了。