skip to Main Content

we have below resource creation and I need to control only the creating of this orahome resource based on var.azure_db_server_create_orahome_disk when its false it should not create and when true it should create.

`resource "azurerm_managed_disk" "dbhome" {
      count                = var.azure_db_server_create_orahome_disk ? 1 : 0
      name                 = "${local.db_server_name}-orahome-0"
      location             = data.azurerm_resource_group.rg.location
      resource_group_name  = data.azurerm_resource_group.rg.name
      storage_account_type = "Premium_LRS"
      create_option        = "Empty"
      disk_size_gb         = var.dbdisksize_dbhome
      zones                = local.availability_zone
    }`

So by adding the line count = var.azure_db_server_create_orahome_disk ? 1 : 0 its hapenning properly and fine.Please correct if this method also not correct.

Problem is, while VM creation its still trying to attached that disk and find a managed_disk_id even I put count = var.azure_db_server_create_orahome_disk ? 1 : 0 on it. I need to fully ignore that disk creation and attachment and continue but other disk should proceed as same as earlier.

`resource "azurerm_virtual_machine" "oracle19c" {
      name                             = local.db_server_name
      location                         = azurerm_network_interface.nic.location
      resource_group_name              = data.azurerm_resource_group.rg.name
      network_interface_ids            = [azurerm_network_interface.nic.id]
      vm_size                          = contains(var.vm_nonsupported_regions, var.region) ? var.dbvmsize_fallback : var.dbvmsize
      delete_os_disk_on_termination    = true
      delete_data_disks_on_termination = true
      
      storage_data_disk {
        count           = var.azure_db_server_create_orahome_disk ? 1 : 0
        name            = "${local.db_server_name}-orahome-0"
        caching         = "ReadOnly"
        managed_disk_id = azurerm_managed_disk.dbhome.id
        create_option   = "Attach"
        lun             = 1
        disk_size_gb    = var.dbdisksize_dbhome
      }
    }`

I added count = var.azure_db_server_create_orahome_disk ? 1 : 0 in azurerm_managed_disk creation and also attachment of that disk. but no luck with the main requirment

2

Answers


  1. Chosen as BEST ANSWER

    Issue has been resloved by making dynamic "storage_data_disk" {} and also with for_each inside that.


  2. I need to control only the creating of this orahome resourcebasedonvar.azure_db_server_create_orahome_disk when its false it should not create and when true it should create.

    By using for_each, we can resolve the same problem.

    The managed disk is created, and the data disk is attached. If var.azure_db_server_create_orahome_disk is false, neither the managed disk is created nor the data disk is attached. The use of the for_each block helps control the number of instances based on the condition.

    I am trying to create an Azure VM with a managed data disk using the following code.

        provider "azurerm" {
          features {}
        }
        variable "azure_db_server_create_orahome_disk" {
          type    = bool
          default = true  
        }
        variable "dbdisksize_dbhome" {
          type    = number
          default = 128
        }
        variable "dbvmsize" {
          type    = string
          default = "Standard_DS2_v2"  
        }
        variable "dbvmsize_fallback" {
          type    = string
          default = "Standard_DS1_v2"  
        }
        locals {
          db_server_name   = "my-db-server"  
          availability_zone = ["1"]  
        }
        resource "azurerm_resource_group" "example" {
          name     = "vm-resources"
          location = "East US"
        }
        resource "azurerm_virtual_network" "main" {
          name                = "vm-network"
          address_space       = ["10.0.0.0/16"]
          location            = azurerm_resource_group.example.location
          resource_group_name = azurerm_resource_group.example.name
          depends_on = [ azurerm_resource_group.example ]
        }
        
        resource "azurerm_subnet" "internal" {
          name                 = "internal"
          resource_group_name  = azurerm_resource_group.example.name
          virtual_network_name = azurerm_virtual_network.main.name
          address_prefixes     = ["10.0.2.0/24"]
          depends_on = [ azurerm_virtual_network.main ]
        }
        
        resource "azurerm_network_interface" "main" {
          name                = "vm-nic"
          location            = azurerm_resource_group.example.location
          resource_group_name = azurerm_resource_group.example.name
        
          ip_configuration {
            name                          = "testconfiguration1"
            subnet_id                     = azurerm_subnet.internal.id
            private_ip_address_allocation = "Dynamic"
          }
          depends_on = [ azurerm_subnet.internal ]
        }
        
        resource "azurerm_managed_disk" "dbhome" {
          count                = var.azure_db_server_create_orahome_disk ? 1 : 0
          name                 = "${local.db_server_name}-orahome-0"
          location             = azurerm_resource_group.example.location
          resource_group_name  = azurerm_resource_group.example.name
          storage_account_type = "Premium_LRS"
          create_option        = "Empty"
          disk_size_gb         = var.dbdisksize_dbhome
          depends_on = [ azurerm_network_interface.main ]
        }
        
        resource "azurerm_virtual_machine" "oracle19c" {
          name                             = "venkatvm1"
          location                         = azurerm_resource_group.example.location
          resource_group_name              = azurerm_resource_group.example.name
          network_interface_ids            = [azurerm_network_interface.main.id]
          vm_size                          = "Standard_D8s_v3"
          delete_os_disk_on_termination    = true
          delete_data_disks_on_termination = true
        
         storage_os_disk {
            name              = "vm-os-disk"
            caching           = "ReadWrite"
            managed_disk_type = "Standard_LRS"
            create_option     = "FromImage"
            disk_size_gb      = 64
          }
        storage_image_reference {
            publisher = "SUSE"
            offer     = "sles-sap-12-sp5"
            sku       = "gen1"
            version   = "latest"
          }
          dynamic "storage_data_disk" {
            for_each = azurerm_managed_disk.dbhome
            content {
              name              = "${local.db_server_name}-orahome-0"
              caching           = "None"
              managed_disk_id   = try(storage_data_disk.value.id, null)
              create_option     = "Attach"
            # managed_disk_type = ""
              lun             = 1
              disk_size_gb    = var.dbdisksize_dbhome
            }
          }
        
          os_profile {
            computer_name  = "venakt-vm"
            admin_username = "Admin"
            admin_password = "Pa$$word@123$"
          }
        
          os_profile_linux_config {
            disable_password_authentication = false
          }
        }
    

    Terraform Apply:

    enter image description here

    When I set default = true, a managed disk is created and attached to the VM as below.

    enter image description here

    When I set default = false, a managed disk is not created

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search