How find out the IP addresses of other instances in a Chef recipe

A very common task in a Chef recipe is to find out information about other instances of your cluster, most importantly the IP address.

This is for example how the build-in support for Rails database.yml works. A Chef recipe is run on the "configure" event on all Rails application servers. If finds out what the current IP address of the database server is and renders the database.yml template with it.

All this information is available in the node[:scalarium] data structure. You can find a complete example here

The most important fields are:

node[:instance]

This will return information about the instance where your Chef script is running. Example are its IP addresses, the Amazon instance type or its roles.

node[:roles]

This will return information about all available roles of the Cloud/cluster and their instances. If you wanted for example to find out what the IP address of the first available database master server is:

db_server = node[:scalarium][:roles]['db-master'][:instances].keys.first
puts node[:scalarium][:roles]['db-master'][:instances][db_server][:private_ip] # => 10.226.11.78
puts node[:scalarium][:roles]['db-master'][:instances][db_server][:ip] # => 70.125.78.113

Please consult the example node[:scalarium] for more examples.