04
02
10
Nodes 2 Hosts
As MaxMedia’s virtualized infrastructure grows, I’ve found myself tiring of looking up IP addresses every time I need to SSH into a box. Deciding it was time to work smarter, I threw together a quick ruby script that will generate a local hosts file that maps a nodes ip address to it’s fully qualified domain name (fqdn). This task was a piece of cake with Knife (Chef’s command-line utility), the Chef server search index, and a little ruby-foo:
#!/usr/bin/env ruby
require ‘rubygems’
require ‘json’
require ‘chef’
hosts = ”# Host File generated from Chef Search on #{Time.now}\n”
# perform search via knife and loop through results
JSON.parse(%x(knife list_nodes)).each do |n|
# parse result as Chef::Node..uses the result json_type
node = JSON.parse(%x(knife show_node —node=#{n}))
# extract ipaddress and fqdn
hosts « “#{node.ipaddress}\t\t#{node.fqdn}\n”
end
# append our localhost info
hosts « “127.0.0.1\t\tlocalhost
255.255.255.255\t\tbroadcasthost
::1\t\t\tlocalhost
fe80::1%lo0\t\tlocalhost\n”
puts hosts
Just paste the output into your Mac’s /etc/hosts file and your ready to roll.