Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 979
Matej Avatar asked Nov 12 '12 00:11

Matej


1 Answers

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.

like image 81
Tim Potter Avatar answered Sep 28 '22 04:09

Tim Potter