Chef搜索生成的节点名称不能在配方中使用

时间:2016-12-16 10:16:26

标签: chef

我正在尝试使用大厨搜索某些节点:

Cookie

第一个hosts = search(:node, 'chef_environment:STG AND role:elasticsearch') Chef::Log.warn("Hosts are #{hosts}") hosts.each do |node| Chef::Log.warn("hostname is: #{node['name']} and has the following tags #{node["tags"]}") end 的结果是:

Chef::Log.warn

[2016-12-16T10:09:55+00:00] WARN: Hosts are [#<Chef::Node:0x00000002e0acc0 @chef_server_rest=nil, @name="tels01", @chef_environment="STG", @primary_runlist=#<Chef::RunList:0x00000002e0ac48 @run_list_items=[#<Chef::RunList::RunListItem:0x00000002df1568 @version=nil, @type=:role, @name="base">, #<Chef::RunList::RunListItem:0x00000002df0de8 @version=nil, @type=:role, @name="elasticsearch">]>, @override_runlist=#<Chef::RunList:0x00000002e0ab80 @run_list_items=[]>, @policy_name=nil, @policy_group=nil, @attributes=#<Chef::Node::Attribute @default={}, @env_default={}, @role_default={}, @force_default={}, @normal={"tags"=>["linux", "els"]}, @override={}, @role_override={}, @env_override={}, @force_override={}, @automatic={}, @merged_attributes=nil, @properties=nil>, @run_state={}>, ... 的结果是

Chef::Log.warn("hostname is: #{node['name']} and has the following tags #{node["tags"]}")

所以我无法获取查询产生的机器的主机名。我也试过[2016-12-16T10:09:55+00:00] WARN: hostname is: and has the following tags ["linux", "els"] [2016-12-16T10:09:55+00:00] WARN: hostname is: and has the following tags ["linux", "els"] [2016-12-16T10:09:55+00:00] WARN: hostname is: and has the following tags ["linux", "elasticsearch"] [2016-12-16T10:09:55+00:00] WARN: hostname is: and has the following tags ["linux", "elasticsearch"] [2016-12-16T10:09:55+00:00] WARN: hostname is: and has the following tags ["linux", "elasticsearch"] ,但结果相同

我做错了什么? 谢谢。

编辑:

感谢您的回复。使用#{node['hostname']}结果相同:els_hosts = search(:node, 'chef_environment:STG AND role:elasticsearch') Chef::Log.warn("Hosts are #{els_hosts}") els_hosts.each do |host| Chef::Log.warn("hostname is: #{host["fqdn"]} and has the following tags #{host["tags"]}") end

2 个答案:

答案 0 :(得分:0)

那应该是node.name。名称是真实的方法,而不是属性。

答案 1 :(得分:0)

问题实际上来自于我以前下载节点定义,删除所有ohai属性并且只保留env属性,运行列表和名称的事实。这就是我无法得到这个名字的原因。

谢谢大家的答案。 加布里埃尔