有条件地标记 Chef 节点

Conditionally tagging a Chef node

使用 Chef 食谱,我想让一个节点发出 http 请求。如果请求失败,我想记录它并让节点用失败代码标记自己。

ruby_block 'connectivity_precheck' do
  block do
    Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
    command = '/bin/curl -o /tmp/connectivity_check.txt --silent --connect-timeout 30 -k https://host.domain.com:4890'
    command_out = shell_out(command)
    if ::File.exist?('/tmp/connectivity_check.txt')
      Chef::Log.info("Connectivity confirmed.")
    else
      Chef::Log.info("Connectivity failed.")
      ???Command to Tag???
    end
  end
  action :create
end

因为我使用的是 ruby 块,所以我不能使用 "tag"。我可以在 ruby 块中做什么来标记节点?

未测试,但我会尝试:

run_context = Chef::RunContext.new(node, {})
run_context.node.tag("failed_tag")

也许更简单:

node.tag("failed_tag")

也有效。 node 应该在 ruby_block 内可用。