Ansible – Find first data disk on a Ubuntu VM running on Azure

If you provision Ubuntu VMs on Azure which have an additional data disk, you can not be sure that the data disk is /dev/sdb.

The reason is that there are Azure VMs sizes which by default have a temporary storage attached. Sometimes those temporary storage disks are assigned earlier and become /dev/sdb. As a result your assigned data disk become /dev/sdc. This happens randomly and can not be influence during provision.

This means if you want to fully automate the partitioning and volume creation with Ansible then you need to detect the actual data disk.

The good news is: In Azure data disks are simlinked /dev/disks/azure.

The following Ansible playbook detects your first data disk in an Ubuntu VM.

- hosts: ubuntu
  become: true

    # BEGIN Azure Hack ------------
    # In Azure datadisks are symlinked in /dev/disks/azure 
    - name: find first data disk on Azure VM (!!! Warning - HACK !!!)
      shell: | 
        find /dev/disk/azure -name 'lun[0123456789]' | sort | sed -n 1p | xargs readlink -f
      register: disk_result
      changed_when: false
      ignore_errors: yes

    - name: debug disk_result.stdout
        var: disk_result.stdout

    - name: debug disk_result.stdout | basename
        var: disk_result.stdout | basename
    - name: set disk_first facts if disk_result.stdout NOT empty
        disk_first_path: "{{ disk_result.stdout }}"
        disk_first_device_name: "{{ disk_result.stdout | basename }}"
      when: disk_result.stdout != ""

    - name: set disk_first facts to /dev/sdb if disk_result.stdout empty
        disk_first_path: /dev/sdb
        disk_first_device_name: sdb
      when: disk_result.stdout == ""

    - name: let's see if it works ...
        msg: "Disk Size: {{ ansible_facts['devices'][disk_first_device_name]['size'] }}"

    # END Azure Hack ------------