Matej
Matej

Reputation: 7627

Get IP address of node returned by chef search in recipe

How can I get IP address returned by node search in chef recipe (ruby).

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

Few weeks ago this code returned IP of first instance from search API.

version: Chef: 10.14.2

Upvotes: 8

Views: 22608

Answers (1)

Tim Potter
Tim Potter

Reputation: 2457

I'm guessing that you're new to Ruby. If so, welcome!

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

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

This should give you the information you need.

Upvotes: 10

Related Questions