Puppet Facts Detecting Cloud Providers for Windows VMs

I’m currently working on a Puppet Module for Windows Server. This module needs to detect which public cloud platform is the Windows server running on. More specifically, Azure, or GCP or AWS.

To do so, I can either write a custom Puppet fact in Ruby, or an external fact (i.e. in PowerShell). So I’ve written both.

The custom fact (cloud.rb) is placed in the lib/facter folder in the module. The external fact (cloud.ps1) is placed in the facts.d folder in the module.

Custom Fact:

External Fact:

To test, you can add a debug message in your Puppet manifest:

#Custom Fact:

$cloud_provider = $::cloud['provider'],

notify{"cloud provider: ${cloud_provider}":}

#External Fact:

$cloud_provider_1 = $::cloud_provider,

notify{"cloud provider PS: ${cloud_provider_1}":}

On the Puppet agent, when you apply the config using –debug flag, you will see it in the output:

image

image

So how does it work? for GCP and AWS, it’s pretty easy. All I needed to check is the VM serial number from the Win32_Bios WMI class. The AWS VM serial number starts with “ec2”, and the GCP VM starts with “GoogleCloud”.

Azure VM is a bit of complicated. You won’t be able to differentiate Hyper-V VM or Azure VM by querying WMI. However, Azure VMs are shipped with a built-in REST Endpoint called Azure Instance Metadata service (https://docs.microsoft.com/en-us/azure/virtual-machines/linux/instance-metadata-service). By using querying this local endpoint on an Azure VM, you can retrieve metadata of the VM, i.e. location, resource Id, resource Group, etc. So the Puppet facts I developed simply query this endpoint, if the HTTP response code is 200, then it’s an Azure VM.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: