I have successfully used this terraform dynamic block to create X number of additional data disks in an Azure Windows VM:
dynamic "storage_data_disk" {
for_each = range(var.nb_data_disk)
content {
name = "${var.vm_hostname}-datadisk-${count.index}-${storage_data_disk.value}"
create_option = "Empty"
lun = "${storage_data_disk.value+10}"
disk_size_gb = 20
managed_disk_type = "Standard_LRS"
}
}
Now I need to format them using the following naming convention for the drives:
DATA , DATA 2 , DATA 3 , DATA 4 , DATA 5 ...........etc
I have the following PowerShell script but do not know how to iterate through the X number of disks created:
Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -UseMaximumSize -DriveLetter F | Format-Volume -FileSystem NTFS -NewFileSystemLabel "data" -Confirm:$false"
I plan to use it as a vm extension in my tf configuration like this:
settings = <<SETTINGS
{
"commandToExecute": "powershell -command \"[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String('${base64encode(data.template_file.tf.rendered)}')) | Out-File -filepath FormatDisk.ps1\" && powershell -ExecutionPolicy Unrestricted -File FormatDisk.ps1"
}
SETTINGS
I need the PowerShell script to name the disks as described above(DATA , DATA 2 , DATA 3 , DATA 4 , DATA 5 ...........etc) i.e it will increment as it iterate through the list of disks created.
Note: The number of disks varies each time.