Skip to content

Instantly share code, notes, and snippets.

@schisamo
Created February 5, 2010 02:40
Show Gist options
  • Save schisamo/295436 to your computer and use it in GitHub Desktop.
Save schisamo/295436 to your computer and use it in GitHub Desktop.
#!/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.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment