Get IP address of node returned by chef search in recipe
Asked Answered
C

1

8

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

Catamaran answered 12/11, 2012 at 0:52 Comment(0)
H
10

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.

Hannan answered 12/11, 2012 at 6:19 Comment(3)
I think by saying "this code returned IP of first instance from search API" Matej meant exactly the same.Igor
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.Underwrite
You can get a list of all nodes using a different search command. Try search(:node, "*:*").Hannan

© 2022 - 2024 — McMap. All rights reserved.