Created
May 10, 2016 18:10
-
-
Save maguec/36d91925949f1790d580d5e3f19a5eda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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