Skip to content

Instantly share code, notes, and snippets.

@ijin
Created June 22, 2014 15:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ijin/3adf07644bc143d771da to your computer and use it in GitHub Desktop.
Save ijin/3adf07644bc143d771da to your computer and use it in GitHub Desktop.
dynamic inventory script for ansible using consul
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
output = {}
s_json = JSON.parse(Net::HTTP.get_response(URI.parse('http://localhost:8500/v1/catalog/services')).body)
services = s_json.keys.reject{|k| k == 'consul'}
services.each do |srv|
res = Net::HTTP.get_response(URI.parse("http://localhost:8500/v1/catalog/service/#{srv}")).body
json = JSON.parse(res)
a = []
json.each {|j| a << j['Address'] }
output = output.merge({srv => a})
end
puts JSON.generate(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment