Skip to content

Instantly share code, notes, and snippets.

@rafaelfelix
Forked from drohr/ec2tags.rb
Last active January 17, 2020 08:55
Show Gist options
  • Select an option

  • Save rafaelfelix/5937611 to your computer and use it in GitHub Desktop.

Select an option

Save rafaelfelix/5937611 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")
region = Facter.value("ec2_placement_availability_zone")[0..-2]
tags = Facter::Util::Resolution.exec("aws ec2 describe-tags --filters \"name=resource-id,values=#{instance_id}\" --region #{region} | 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
@edsonmarquezani

Copy link
Copy Markdown

In case you are using an stock version of Facter, that version gets 'region' and 'instanceId' from metadata service directly, but thus, depends on 'curl'.

https://gist.github.com/edsonmarquezani/430509169fe914217367

@feniix

feniix commented Aug 23, 2015

Copy link
Copy Markdown

Since we are at it, this is my version 🎱
https://gist.github.com/feniix/eb9cb233f903f62280d6

@butlern

butlern commented Jun 28, 2016

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment