Amazon EC2 Facter Recipe
Here is a facter script to get all the metadata from Amazon EC2.
These facts were put into mainline Facter in version 1.5.5.
# Copyright 2008 Tim Dysinger
# Distributed under the same license as Facter
# 27.02.09 KurtBe
# Added a can_connect? function so that this fact can safely be distributed to non-ec2 instances
# otherwise the script hangs if the amazon-ip is not reachable
# 13.03.09 Francois Deppierraz
# Fixed the timeout handling code because which was not actually working. A
# file named "169.254.169.254" was created instead.
require 'open-uri'
require 'timeout'
def metadata(id = "")
open("http://169.254.169.254/2008-02-01/meta-data/#{id||=''}").read.
split("\n").each do |o|
key = "#{id}#{o.gsub(/\=.*$/, '/')}"
if key[-1..-1] != '/'
value = open("http://169.254.169.254/2008-02-01/meta-data/#{key}").read.
split("\n")
value = value.size>1 ? value : value.first
symbol = "ec2_#{key.gsub(/\-|\//, '_')}".to_sym
Facter.add(symbol) { setcode { value } }
else
metadata(key)
end
end
end
begin
Timeout::timeout(1) { metadata }
rescue Timeout::Error
puts "ec2-metadata not loaded"
end
which results in something like the following:
$ec2_ami_id
$ec2_ami_launch_index
$ec2_ami_manifest_path
$ec2_block_device_mapping_ami
$ec2_block_device_mapping_ephemeral0
$ec2_block_device_mapping_root
$ec2_block_device_mapping_swap
$ec2_hostname
$ec2_instance_id
$ec2_instance_type
$ec2_kernel_id
$ec2_local_hostname
$ec2_local_ipv4
$ec2_placement_availability_zone
$ec2_public_hostname
$ec2_public_ipv4
$ec2_public_keys_0_openssh_key
$ec2_reservation_id
$ec2_security_groups
