如何在通过 Terraform 创建 VMSS 期间在值为 0 时跳过存储数据磁盘创建

How to skip storage data disk creation when value is 0 during VMSS creation through Terraform

我已经尝试并成功地同时使用 os_disk 和 data_disk 创建了 VMSS。但我有要求,比如当 "data_disk==0" 的值时,我需要跳过 Data_disk 的创建。我试过使用值 0 但它显然要求我在 (1-32767) 之间设置值。这是我成功的代码

provider "azurerm" {
    features{}
}

resource "azurerm_resource_group" "vmss" {
    name     = var.resourcegroup
    location = var.location
}

resource "azurerm_windows_virtual_machine_scale_set" "vmss" {
  name                 = var.vmss_name
  resource_group_name  = azurerm_resource_group.vmss.name
  location             = azurerm_resource_group.vmss.location
  sku                  = var.vm_sku
  instances            = var.instancescount
  computer_name_prefix = var.computer_name_prefix
  admin_password       = var.vmpassword
  admin_username       = var.vmusername
  provision_vm_agent   = true
  overprovision        = false

  source_image_reference {
    publisher = var.publisher
    offer     = var.offer
    sku       = var.os_sku
    version   = var.image_version
  }

  os_disk {
    storage_account_type = var.os_disk_type
    caching              = "ReadWrite"
    disk_size_gb         = var.os_disk_size
  }

  data_disk{
    lun                  = 1
    storage_account_type = var.data_disk_type
    caching              = "ReadWrite"
    disk_size_gb         = var.data_disk_size
  }

    winrm_listener {
      protocol = "Http"
    }

    additional_unattend_content{
      content      = "<AutoLogon><Password><Value>${var.vmpassword}</Value></Password><Enabled>true</Enabled><LogonCount>1</LogonCount><Username>${var.vmusername}</Username></AutoLogon>"
      setting      = "AutoLogon"
  }
    # Unattend config is to enable basic auth in WinRM, required for the provisioner stage.
    additional_unattend_content {
        setting      = "FirstLogonCommands"
        content      = file("./files/FirstLogonCommands.xml")
    }

  network_interface {
    name    = "${var.computer_name_prefix}-profile"
    primary = true
    ip_configuration {
        name      = "${var.computer_name_prefix}-IPConfig"
        primary   = true
        subnet_id = var.subnet
    }
  }
}

任何人都可以帮助我在值为 0 时跳过 data_disk 创建。

提前致谢。

谢谢, 湿婆

在这种情况下,您可以使用 Conditional Expressions 格式 condition ? true_val : false_val

像这样声明一个变量,

variable "select_data_disk" {
  description = "conditionally creating vmss with data disk"
  default = true // when the var is true, data disk is selected; when the var is false, data disk is not selected.
}

然后创建两个resource "azurerm_windows_virtual_machine_scale_set" "example1"resource "azurerm_windows_virtual_machine_scale_set" "example2"。一个VMSS资源包括数据磁盘块,另一个不包括数据磁盘块。在每个资源中使用 count = var.select_data_disk ? 0 : 1

例子

variable "select_data_disk" {
  description = "conditionally creating vmss with data disk"
  default = true // when the var is true, data disk is selected; when the var is false, data disk is not selected.
}

...



resource "azurerm_windows_virtual_machine_scale_set" "example" {
  count = var.select_data_disk ? 1 : 0
  name                = "myvmss"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku                 = "Standard_F2"
  instances           = 1
  admin_password      = "P@55w0rd1234!"
  admin_username      = "adminuser"

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter-Server-Core"
    version   = "latest"
  }

  os_disk {
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }


    data_disk{
    lun                  = 1
    storage_account_type = "Standard_LRS"  //Standard_LRS, StandardSSD_LRS, Premium_LRS and UltraSSD_LRS.
    caching              = "ReadWrite"
    disk_size_gb         = 1
  }


  network_interface {
    name    = "example"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
      subnet_id = azurerm_subnet.internal.id
    }
  }
}


resource "azurerm_windows_virtual_machine_scale_set" "example1" {
  count = var.select_data_disk ? 0 : 1
  name                = "myvmss"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  sku                 = "Standard_F2"
  instances           = 1
  admin_password      = "P@55w0rd1234!"
  admin_username      = "adminuser"

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter-Server-Core"
    version   = "latest"
  }

  os_disk {
    storage_account_type = "Standard_LRS"
    caching              = "ReadWrite"
  }



  network_interface {
    name    = "example"
    primary = true

    ip_configuration {
      name      = "internal"
      primary   = true
      subnet_id = azurerm_subnet.internal.id
    }
  }
}