Ansible – Detect if your Ubuntu VM is running on Azure

On different hypervisor platforms VMs have different properties. For example the naming of network interface is different. Sometimes the ordering of virtual disk is different, which results also in different device naming. Therefor it is necessary to detect the underlying hypervisor platform the VM is running on.

The following Ansible playbook uses variables which are populate by gather_facts to determine if the VM is running on Azure.

---
- hosts: ubuntu

  tasks:

    - name: Is on Azure?
      debug:
        msg: "yes"
      when: ((ansible_board_vendor == "Microsoft Corporation") or (ansible_chassis_vendor == "Microsoft Corporation") or (ansible_kernel is search("azure")))

    - name: Is on Azure?
      debug:
        msg: "no"
      when: 
        - ansible_board_vendor != "Microsoft Corporation"
        - ansible_chassis_vendor != "Microsoft Corporation"
        - not ansible_kernel is search("azure")