As a person who uses Puppet Enterprise in ec2 heavily, I've spent a lot of 
time thinking about, and working around the present ec2 facts. Right now, 
ec2 facts are merely a flattened version of the ec2 instance metadata, and 
while it provides quite a lot of information, and a good bit of it being 
information on would desire, sometimes that information is difficult to 
access (possibly my ignorance at work). As an example, the fact for the VPC 
id of an ec2 instance, is ec2_network_interfaces_macs_*$Mac address 
of interface_*vpc_id. Although I've not had to consider the implications of 
many interfaces attached to an instance, this fact is unwieldy, but often a 
good item to turn configuration on. I've dealt with it, using a simpler 
custom fact.

require 'facter'

  Facter.add("ec2_vpc_id") do
    confine :cloud_provider => 'aws'
    setcode do
      mac    = Facter.value(:ec2_mac)
      vpc_id_fact = "ec2_network_interfaces_macs_#{mac}_vpc_id"
      Facter.value(vpc_id_fact)
    end
  end


Forgive the cloud_provider confine, it's a relic of the pre factor 2 in PE 
days, and I haven't gone back to find the appropriate confine for VPC 
instances yet.

Anyway, my question is, in general, does it seem a worthwhile effort to 
refactor the base ec2 facts to use parts of the meta data, instead of the 
more simple flattening of the entire return (which would also remove some 
of the undesirable facts created). Also has thought been given to using the 
ruby SDK to invoke describe methods to get new facts into the core. Of 
particular interest might be a fact, or set of facts around the ec2 
instance tags, which are not in the metadata. Below is my hacky way of 
accomplishing that at present.

require 'facter'
require 'json'
cloud_provider = Facter.value(:cloud_provider)
case cloud_provider
  when 'aws'
    instance_id = Facter.value(:ec2_instance_id)
    osfamily = Facter.value(:osfamily)
    case osfamily
      when 'Debian'
        tags = Facter::Core::Execution.exec("/usr/local/bin/aws ec2 
describe-tags --filters \"Name=resource-id,Values=#{instance_id}\" --output 
json --region us-east-1")
      when 'RedHat'
        tags = Facter::Core::Execution.exec("/usr/bin/aws ec2 describe-tags 
--filters \"Name=resource-id,Values=#{instance_id}\" --output json --region 
us-east-1")
    end
      tags_hash = JSON.parse(tags)["Tags"]
    begin
      tags_hash.each do |tag|
    rescue
        Facter.add("ec2_tag_" + tag["Key"]) do
          setcode do
        tag["Value"]
          end
        end
      end
    end
end


Please forgive any ignorance to history, convention, or customs, this is my 
first post, and I'm pretty new to the world of writing anything in ruby. 
Thoughts, advice, direction would all be appreciated.

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-dev/6105274e-752b-41c5-b688-4c485a91da35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to