csi
csi

Reputation: 9338

Get specific IP address for each Chef server node

Please excuse as I am new to both Chef & Ruby. This should be an easy question. Following along with Jason Grimes's Tutorial.

How do I remove localhost and instead use the specific IP address of a Chef node in the code below?

# Get a list of web servers
webservers = node['roles'].include?('webserver') ? [{'ipaddress' => 'localhost'}] : search(:node, "role:webserver AND chef_environment:#{node.chef_environment}")

Can I instead substitute
webservers = search(:node, "role:webserver AND chef_environment:#{node.chef_environment}")
since IP_address is an automatic attribute according to Opscode and this StackOverflow question.

Unfortunately I don't quite understand how I would test this to be sure otherwise I would have run a test. Thanks!

Upvotes: 2

Views: 1694

Answers (1)

Draco Ater
Draco Ater

Reputation: 21226

Yes, you are right.

webservers = search(:node, "role:webserver AND chef_environment:#{node.chef_environment}")

then webservers will be an array of nodes and you will be able to obtain IP by

webservers[index]['ipaddress']

Upvotes: 2

Related Questions