Created
January 8, 2015 10:12
-
-
Save paulovittor23/819c2929d09aa7487f7e to your computer and use it in GitHub Desktop.
ec2 tags as facts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'facter' | |
require 'json' | |
instance_id = Facter::Util::Resolution.exec("ec2-metadata -i | awk '{print $2}'") | |
unless instance_id.nil? | |
region = Facter::Util::Resolution.exec("ec2-metadata -z | awk '{print $2}'") | |
unless region.nil? | |
region = region[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}]'") | |
unless tags.nil? | |
parsed_tags = JSON.parse(tags) | |
parsed_tags.each do |tag| | |
fact = "ec2_tag_#{tag["key"]}".downcase | |
Facter.add(fact) { setcode { tag["value"] } } | |
end | |
else | |
notice("[ec2tags] não foi possível recuperar as tags desta máquina"); | |
end | |
else | |
notice("[ec2tags] não foi possível recuperar a região desta máquina"); | |
end | |
else | |
notice("[ec2tags] não foi possível recuperar o ID de instância desta máquina"); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment