Skip to content

Instantly share code, notes, and snippets.

@toddlers
Created July 8, 2013 14:38
Show Gist options
  • Save toddlers/5949389 to your computer and use it in GitHub Desktop.
Save toddlers/5949389 to your computer and use it in GitHub Desktop.
Hack to get ec2 tags to facter. Depends on aws-cli (https://github.com/aws/aws-cli), jq (http://stedolan.github.io/jq/) and the JSON RubyGem (http://rubygems.org/gems/json)
require 'facter'
require 'json'
if Facter.value("ec2_instance_id") =! nil
instance_id = Facter.value("ec2_instance_id")
tags = Facter::Util::Resolution.exec("aws ec2 describe-tags --filters \"name=resource-id,values=#{instance_id}\" --region sa-east-1 | jq '[.Tags[] | {key: .Key, value: .Value}]'")
parsed_tags = JSON.parse(tags)
parsed_tags.each do |tag|
fact = "ec2_tag_#{tag["key"]}"
Facter.add(fact) { setcode { tag["value"] } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment