0
votes

I've tried and successfully created VMSS with os_disk and data_disk together. but i've requirement like when value of "data_disk==0" then i need to skip the creation of Data_disk. I've tried with value 0 but its clearly asking me to set value between(1-32767) . Here is my successful code

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
    }
  }
}

Can anyone help me to skip the data_disk creation when the value is 0.

Thanks in Advance.

Thanks, Siva

1
Terraform supports conditional expressions of the format <CONDITION> ? <TRUE_VAL> : <FALSE_VAL>. This ternary syntax, which may be familiar to you from other programming languages, will evaluate the boolean logic in CONDITION, and if the result is true, it will return TRUE_VAL, and if the result is false, it’ll return FALSE_VAL.Eldho
@Eldho That won't work for removing a block. You'd need to use a dynamic block here instead I think.ydaetskcoR
Yes i couldn't find anything relative to dynamic blockEldho
@ydaetskcoR , if possible could you please provide sample links which can help me to solve this issueSiva Sankara Rao Bacha
@here any solutions .. please help me on thisSiva Sankara Rao Bacha

1 Answers

1
votes

In this case, you could use Conditional Expressions of the format condition ? true_val : false_val

declare a variable like this,

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.
}

Then create the two resource "azurerm_windows_virtual_machine_scale_set" "example1" and resource "azurerm_windows_virtual_machine_scale_set" "example2". One VMSS resource includes the data disk block, another does not include the data disk block. Use count = var.select_data_disk ? 0 : 1 in each resource.

Example

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
    }
  }
}