#Rebuild hosts file from knife node list, require 'erb' require 'json' require 'ohai' #to find my own IP def myaddress thissys=Ohai::System.new thissys.all_plugins myip=thissys['cloud']['private_ips'][0] return myip end #This is called in the template file by erb def findremoteip(name=nil) return "#No such name" if name.nil? svrname=name+'.somedomain.com' nodes.search("name:#{svrname}") do |thisnode| return "#{thisnode['cloud']['private_ips'][0]} #{name} #{svrname}" if thisnode['fqdn']==svrname end return "# No nodes entry for "+svrname end template='/etc/hosts.erb' #These are used in the template. scriptname="#{$0} #{$1}" myip_ohai=myaddress abort "Could not find my IP?" if myip_ohai.nil? erb=ERB.new File.new(template).read,nil, '<>' puts erb.result(binding)