ChisBlog

agile infrastructure and life

Nodes 2 Hosts

| Comments

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 node list)).each do |n|
  # parse result as Chef::Node..uses the result json_type
  node = JSON.parse(%x(knife node show #{n}))
  # extract ipaddress and fqdn
  hosts << "#{node.ipaddress}\t\t#{node.hostname}\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.

Comments