Last active
July 28, 2022 21:26
-
-
Save ColOfAbRiX/48f6e70014b8db972caa1d958d482613 to your computer and use it in GitHub Desktop.
Ansible gist to detect the cloud platform on which a VM is running
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
--- | |
# | |
# Detects the cloud provider on which the VM is running and sets accordingly the | |
# following variables: | |
# cloud_platform_is_aws=true when on AWS, false otherwise | |
# cloud_platform_is_gcp=true when on GCP, false otherwise | |
# cloud_platform_is_azure=true when on Azure, false otherwise | |
# cloud_platform_name: | |
# - 'azure' when on Azure | |
# - 'aws' when on AWS | |
# - 'gcp' when on GCP | |
# - 'unknown' otherwise | |
# | |
- set_fact: | |
cloud_platform_is_aws: false | |
cloud_platform_is_gcp: false | |
cloud_platform_is_azure: false | |
# SEE: https://stackoverflow.com/questions/11570965/how-to-detect-azure-amazon-vm | |
- name: "Detect AWS" | |
block: | |
- name: "Probe for AWS" | |
shell: dmidecode -s bios-version | grep -iq "amazon" | |
changed_when: false | |
failed_when: false | |
register: probe_aws | |
- set_fact: | |
cloud_platform_is_aws: "{{ probe_aws | success and probe_aws.rc == 0 }}" | |
# SEE: https://stackoverflow.com/questions/30911775/how-to-know-if-a-machine-is-an-google-compute-engine-instance | |
- name: "Detect GCP" | |
block: | |
- name: "Probe for GCP" | |
shell: dmidecode -s bios-version | grep -iq "google" | |
changed_when: false | |
failed_when: false | |
register: probe_gcp | |
- set_fact: | |
cloud_platform_is_gcp: "{{ probe_gcp | success and probe_gcp.rc == 0 }}" | |
# SEE: https://stackoverflow.com/questions/11570965/how-to-detect-azure-amazon-vm | |
- name: "Detect Azure" | |
block: | |
- name: "Probe for Azure 1/2" | |
shell: dmidecode -s system-manufacturer | grep -iq "microsoft corporation" | |
changed_when: false | |
failed_when: false | |
register: probe_azure_1 | |
- name: "Probe for Azure 2/2" | |
shell: dmidecode -s system-product-name | grep -iq "virtual machine" | |
changed_when: false | |
failed_when: false | |
register: probe_azure_2 | |
- set_fact: | |
cloud_platform_is_azure: "{{ | |
(probe_azure_1 | success and probe_azure_1.rc == 0) or | |
(probe_azure_2 | success and probe_azure_2.rc == 0) | |
}}" | |
- name: "Set Cloud Platform" | |
set_fact: | |
cloud_platform_name: "{% if cloud_platform_is_azure %}azure{% elif cloud_platform_is_aws %}aws{% elif cloud_platform_is_gcp %}gcp{% else %}unknown{% endif %}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment