Skip to content

Instantly share code, notes, and snippets.

@tyconsulting

tyconsulting/cloud.rb Secret

Created Feb 27, 2020
Embed
What would you like to do?
Puppet custom fact to detect which cloud platform is the Windows server running on
# Custom facters for Windows - Detect Cloud provider
if Facter.value('osfamily') == 'windows'
Facter.add('cloud') do
confine :osfamily => :windows
has_weight 100
setcode do
begin
result={}
require 'win32ole'
require 'net/http'
value = nil
wmi = WIN32OLE.connect("winmgmts:\\\\.\\root\\cimv2")
bios = wmi.ExecQuery("SELECT * FROM Win32_bios").each.first
serialNumber = bios.serialNumber
if serialNumber =~ /^GoogleCloud/
result['provider'] = 'gcp'
elsif serialNumber =~ /^ec2/
result['provider'] = 'aws'
else
uri = URI('http://169.254.169.254/metadata/instance?api-version=2017-08-01')
req = Net::HTTP::Get.new(uri)
req['Metadata'] = 'true'
res = Net::HTTP.start(uri.hostname, uri.port, open_timeout: 1) {|http|
http.request(req)
}
if res.code == '200'
result['provider'] = 'azure'
end
end
rescue
nil
end
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.