[Solved] Dynamically choosing disks to partition in Ansible

Hello, I’m deploying some Linux VMs with terraform and provisioning them with ansible. Each VM has 2 disks, 1 partitioned with lvm for the os install and 1 unpartitioned for data. I want to format and partition the disk using ansible. Unfortunately, hardcoding identifiers (/dev/sdX) doesn’t work in this case because the identifier isn’t consistent across deployments.

Some deployments the data disk is /dev/sda and some deployments it’s /dev/sdb.

Any guidance on how I can dynamically choose which disk to format and partition?

My terraform module currently hardcodes 2 disks. I’ve tried specifying unit_number but it doesn’t have any effect unfortunately.

  disk {
    label            = "disk0"
    size             = var.disk_size
    unit_number      = 0
    eagerly_scrub    = var.eagerly_scrub
    thin_provisioned = var.thin_provisioned
  }
  disk {
    label        = "disk1"
    attach       = true
    unit_number  = 1
    path         = var.vmdk_path
    datastore_id = var.data_datastore_id
  }

I’ve done some testing and discovered that it’s only disk assignments in Linux that are changing, not the LUN ID.

image
Working VM

image
Broken VM

That means that instead of using /dev/sdX, I can use something like

/dev/disk/by-path/pci-0000:02:00.0-scsi-0:0:1:0-part1