Skip to content

Instantly share code, notes, and snippets.

@srinivasmohan
Created December 17, 2012 21:08

Revisions

  1. srinivasmohan created this gist Dec 17, 2012.
    30 changes: 30 additions & 0 deletions updhosts.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #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)