Puppet: System Administration Automated

Support

Amazon EC2 Facter Recipe

Here is a factor script to get all the metadata from Amazon EC2.

# Copyright 2008 Tim Dysinger
# Distributed under the same license as Facter

require 'open-uri'

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

metadata

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