Skip to content

Instantly share code, notes, and snippets.

@yureative
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yureative/b1fe9f093472fd40ce35 to your computer and use it in GitHub Desktop.
Save yureative/b1fe9f093472fd40ce35 to your computer and use it in GitHub Desktop.
A custom Ohai plugin which provides EC2 instance tags
require 'json'
Ohai.plugin(:EC2Tags) do
provides "ec2/tags"
depends "ec2"
collect_data do
region = ec2['placement_availability_zone'].chop
instance_id = ec2['instance_id']
so = shell_out("aws ec2 describe-tags --region #{region} --filters Name=resource-type,Values=instance Name=resource-id,Values=#{instance_id}")
if so.exitstatus == 0
ec2[:tags] = JSON.parse(so.stdout)['Tags'].map {|tag| {tag['Key'] => tag['Value']}}.inject(Mash.new, :merge)
end
end
end
@yureative
Copy link
Author

Path example: /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/ohai-7.4.0/lib/ohai/plugins/ec2_tags.rb

$ ohai ec2/tags
{
  "EIP": "1.1.1.1",
  "Name": "example-host1"
}

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