Get the node IP address returned by the cook search in the recipe

How can I get the IP address returned by node by searching in the chef's recipe (ruby).

dbnodes = search(:node, "role:Db") Chef::Log.info(dbnodes.first["ipaddress"]) # nil 

A few weeks ago, this code returned the IP address of the first instance from the search API.

version: Chef: 10.14.2

+8
chef chef-recipe
source share
1 answer

I assume you are new to Ruby. If yes, welcome!

The Chef search() function returns an array of Chef nodes, and you take the head of this array using the first method. To access the IP address of other nodes, use the usual array operator:

 dbnodes = search(:node, "role:Db") dbnodes.each do |node| Chef::Log.info("#{node["name"]} has IP address #{node["ipaddress"]}") end 

This will give you the necessary information.

+9
source share

All Articles