I am writing a chef provisioning recipe to provision a cluster of machines. I have more/less done that, but now I want to set the hosts file of all of the provisioned machines to be the dynamically assigned IP addresses of all of the other hosts. Is there a way to query what the ip address of a provisioned host is?
2 Answers
Not on ChefProvisioning layer, but I have done this as follows:
You can use hostsfile cookbook from chef supermarket and a databag in chef server.
It is possible to access to one node IP with attribute node[:ipaddress]
, store it inside the databag and then access with a loop through all ips, using hostsfile cookbook to create/update entries with resource hostsfile_entry
, something similar to:
node[:list_of_hosts].each do |name, ip|
hostsfile_entry ip do
hostname name
action [:create_if_missing, :update]
end
end
Assuming there that you have in a hash the list of IPs and hostnames from the databag.
Hope you found this helpful.

- 1,284
- 2
- 17
- 40
-
This is exactly what I needed. Thanks! – Jason Thompson Oct 15 '16 at 04:24
The short answer is: you don't which is one reason you shouldn't be using chef-provisioning. Long answer, some of the drivers allow accessing the underlying object through the resource but what you'll end up with looks nothing like normal Chef code. Store each machine
object in an array and then later you can try to use that for a second pass on things.
The real answer is that the provisioning layer shouldn't be doing this, use something like Chef Search or the relevant cloud API from the recipe on the actual provisioned nodes like you would with "normal" Chef.

- 52,400
- 4
- 52
- 75