Get IP address of node returned by chef search in recipe

22,207

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.

Share:
22,207
Matej
Author by

Matej

Updated on November 12, 2020

Comments

  • Matej
    Matej over 3 years

    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

  • meandre
    meandre about 11 years
    I think by saying "this code returned IP of first instance from search API" Matej meant exactly the same.
  • sandip divekar
    sandip divekar over 9 years
    So for getting ips of nodes we have to assign some roles or environment as pattern to search nodes right ?. Suppose i have not assigned any role or environment just configured 3 clients with server so is there any way to get ips of these clients.
  • Tim Potter
    Tim Potter over 9 years
    You can get a list of all nodes using a different search command. Try search(:node, "*:*").