Skip to content

Instantly share code, notes, and snippets.

@marshyski
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshyski/686b12a5373e741e76d4 to your computer and use it in GitHub Desktop.
Save marshyski/686b12a5373e741e76d4 to your computer and use it in GitHub Desktop.
Creating Puppet Facts by AWS EC2 Tags with a proxy
#Set proxy before we try to talk to AWS API
ENV["http_proxy"] ||= 'http://proxy:port'
ENV["https_proxy"] ||= 'http://proxy:port'
ENV["NO_PROXY"] ||= '127.0.0.1, localhost, 169.254.169.254'
#Make 1 EC2 API call and put the output on a dot file in /tmp/.tags
region = Facter.value('region')
instanceid = Facter.value('instanceid')
%x{/usr/bin/aws --region #{region} --output text ec2 describe-instances --instance-ids #{instanceid} | grep "TAGS" | awk '{ print $2 " " $3 " " $4}'| tr [:upper:] [:lower:] > /tmp/.tags }.chomp
#Create the fact from the NAME (EC2) TAG
Facter.add("tag_name") do
setcode do
%x{/bin/egrep -iw "Name" /tmp/.tags | /bin/awk '{ print $2 }'}.chomp
end
end
#Create the fact from the SDLC (EC2) TAG
Facter.add("tag_sdlc") do
setcode do
%x{/bin/egrep -iw "SDLC" /tmp/.tags | /bin/awk '{ print $2 }'}.chomp
end
end
##Create the fact from the OWNER (EC2) TAG
Facter.add("tag_owner") do
setcode do
%x{/bin/egrep -iw "OWNER" /tmp/.tags | /bin/awk '{ print $2 " " $3 }'}.chomp
end
end
##Create the fact from the PURPOSE (EC2) TAG
Facter.add("tag_purpose") do
setcode do
%x{/bin/egrep -iw "PURPOSE" /tmp/.tags | /bin/awk '{ print $2 }'}.chomp
end
end
#Create the fact from the role (EC2) TAG
Facter.add("tag_role") do
setcode do
%x{/bin/egrep -iw "^role" /tmp/.tags | /bin/awk '{ print $2 }'}.chomp
end
end
#Creates and populates the AWS region fact
Facter.add("region") do
setcode do
Facter::Util::Resolution.exec('/usr/bin/curl -sf http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" \'{print $4}\'')
end
end
#Creates and populates the instanceid fact
Facter.add("instancetype") do
setcode do
Facter::Util::Resolution.exec('/usr/bin/curl -sf http://169.254.169.254/latest/meta-data/instance-type;echo ""')
end
end
#Creates and populates the instanceid fact
Facter.add("instanceid") do
setcode do
Facter::Util::Resolution.exec('/usr/bin/curl -sf http://169.254.169.254/latest/meta-data/instance-id;echo ""')
end
end
#Creates a fact which displays the version (JUST THE VERSION) of the awscli
Facter.add('awscliver') do
setcode do
`/usr/bin/aws --version 2>&1`[/aws-cli\/([^ ]+) /, 1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment