On Mon, Apr 13, 2015 at 4:04 AM, Phil Watts <pwatts...@gmail.com> wrote:

> 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.
>

This particular case may be easier with the structured ec2_metadata fact:

ec2_metadata => {"ami-id"=>"ami-4dbf9e7d", "ami-launch-index"=>"0",
"ami-manifest-path"=>"(unknown)",
"block-device-mapping"=>{"ami"=>"/dev/sda1", "root"=>"/dev/sda1"},
"hostname"=>"ip-10-0-2-134.us-west-2.compute.internal",
"instance-action"=>"none", "instance-id"=>"i-4a26a0bc",
"instance-type"=>"t2.micro",
"local-hostname"=>"ip-10-0-2-134.us-west-2.compute.internal",
"local-ipv4"=>"10.0.2.134", "mac"=>"06:84:3c:20:ab:9e",
"metrics"=>{"vhostmd"=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"},
"network"=>{"interfaces"=>{"macs"=>{"06:84:3c:20:ab:9e"=>{"device-number"=>"0",
"interface-id"=>"eni-89d982ff",
"ipv4-associations"=>{"52.11.17.205"=>"10.0.2.134"},
"local-hostname"=>"ip-10-0-2-134.us-west-2.compute.internal",
"local-ipv4s"=>"10.0.2.134", "mac"=>"06:84:3c:20:ab:9e",
"owner-id"=>"482693910459", "public-hostname"=>"
ec2-52-11-17-205.us-west-2.compute.amazonaws.com",
"public-ipv4s"=>"52.11.17.205", "security-group-ids"=>"sg-b4c4c4d1",
"security-groups"=>"launch-wizard-128", "subnet-id"=>"subnet-c88c2abf",
"subnet-ipv4-cidr-block"=>"10.0.2.0/24", "vpc-id"=>"vpc-2c13bd49",
"vpc-ipv4-cidr-block"=>"10.0.0.0/16"}}}},
"placement"=>{"availability-zone"=>"us-west-2b"}, "profile"=>"default-hvm",
"public-hostname"=>"ec2-52-11-17-205.us-west-2.compute.amazonaws.com",
"public-ipv4"=>"52.11.17.205",
"public-keys"=>{"0"=>{"openssh-key"=>"..."}},
"reservation-id"=>"r-5f98f853", "security-groups"=>"launch-wizard-128",
"services"=>{"domain"=>"amazonaws.com"}}


ec2_metadata.network.interfaces.macs is a hash that can be iterated over in
Puppet. This may make some tasks easier.

A brief example of using them to print the vpc-id for each network
interface:

$macs = $::facts['ec2_metadata']['network']['interfaces']['macs']
each($macs) |$k, $v| { notice $v['vpc-id'] }



> 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.
>

That's an interesting idea, but looks like you need to install the aws
tool. That makes it trickier to have as a core fact.

>  --
> 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
> <https://groups.google.com/d/msgid/puppet-dev/6105274e-752b-41c5-b688-4c485a91da35%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Michael Smith
Sr. Software Engineer, Puppet Labs

*PuppetConf 2015 <http://2015.puppetconf.com/> is coming to Portland,
Oregon! Join us October 5-9.*
*Register now to take advantage of the Early Adopter discount
<https://www.eventbrite.com/e/puppetconf-2015-october-5-9-tickets-13115894995?discount=EarlyAdopter>
*
*—**save $349!*

-- 
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/CABy1mM%2B8MX_WOO__RzLNaGP2w284EpTbV2m8Bs%3DhHKD9Xj7yaw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to