有条件地标记 Chef 节点



使用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

由于我使用的是红宝石块,因此我不能使用"标签"。 我可以在红宝石块中做什么来标记节点?

未测试,但我会尝试:

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

也许更简单:

node.tag("failed_tag")

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

最新更新