Skip to content

Instantly share code, notes, and snippets.

@voondo
Created February 19, 2016 11:11
Show Gist options
  • Save voondo/d362dbf07236f0aff1bf to your computer and use it in GitHub Desktop.
Save voondo/d362dbf07236f0aff1bf to your computer and use it in GitHub Desktop.
Scaleway ansible dynamic inventory
#!/bin/ruby
require 'json'
require 'optparse'
require 'net/http'
require 'pry'
require 'yaml'
auth_token = ENV['SCALEWAY_AUTH_TOKEN']
uri = URI('https://api.scaleway.com/servers')
req = Net::HTTP::Get.new(uri)
req['Content-Type'] = 'application/json'
req['X-Auth-Token'] = auth_token
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
body = res.body
json = JSON.parse body
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-H", "--host HOST", "Host") do |v|
end
opts.on("-l", "--list", "List") do ||
hostvars = Hash[json['servers'].collect do |server|
[
server['private_ip'],
Hash[server.collect {|k,v| ["scaleway_#{k}", v] }]
]
end]
list = json['servers'].each_with_object(Hash.new{|h,k| h[k] = []}) do |h, result|
h['tags'].each { |n| result[n] << h['private_ip'] }
end
list['_meta'] = { 'hostvars' => hostvars }
print JSON.generate(list)
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment