Skip to content

Instantly share code, notes, and snippets.

@maguec
Created May 10, 2016 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maguec/36d91925949f1790d580d5e3f19a5eda to your computer and use it in GitHub Desktop.
Save maguec/36d91925949f1790d580d5e3f19a5eda to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'uri'
require 'yaml'
require 'net/http'
require 'getoptlong'
args = {
:yaml => false,
:json => false,
:param => "ansible_role",
}
opts = GetoptLong.new(
[ '--dump-yaml' , '-y' , GetoptLong::OPTIONAL_ARGUMENT ] ,
[ '--dump-json' , '-j' , GetoptLong::OPTIONAL_ARGUMENT ] ,
[ '--param' , '-p' , GetoptLong::OPTIONAL_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--dump-yaml'
args[:yaml] = true
when '--dump-json'
args[:json] = true
when '--param'
args[:param] = arg
end
end
begin
url = URI.parse('http://169.254.169.254/latest/user-data')
http = Net::HTTP.new(url.host, url.port)
response = http.request(Net::HTTP::Get.new(url.request_uri))
user_data = JSON.parse(response.body)
if args[:yaml]
puts user_data.to_yaml
exit!
elsif args[:json]
puts user_data.to_yaml
exit!
else
puts user_data[args[:param]]
end
rescue Exception => e
STDERR.puts "SCRIPT ERROR: #{e.message}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment