基于变量的Terraform Azure磁盘附件

wtzytmuj  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(90)

我们有下面的资源创建,我需要控制只创建这个orahome资源的基础上var.azure_db_server_create_orahome_disk,当它的假,它不应该创建,当它的真,它应该创建。

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

字符串
所以通过添加行count = var.azure_db_server_create_orahome_disk ? 1 : 0,它的发生正确和罚款。如果这个方法也不正确,请纠正。
问题是,当VM创建时,它仍然试图连接该磁盘并找到一个managed_disk_id,即使我在它上放置了count = var.azure_db_server_create_orahome_disk ? 1 : 0。我需要完全忽略磁盘创建和附件,并继续,但其他磁盘应继续进行相同的先前。

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


我在azurerm_managed_disk创建中添加了count = var.azure_db_server_create_orahome_disk ? 1 : 0,并添加了该磁盘的附件。但主要的要求没有进展

cgvd09ve

cgvd09ve1#

通过创建动态“storage_data_disk”{}并在其中使用for_each解决了问题。

fafcakar

fafcakar2#

我只需要控制这个orahome资源的创建基于var.azure_db_server_create_orahome_disk时,它不应该创建,当它是true时,它应该创建。
通过使用for_each,我们可以解决同样的问题。
创建托管磁盘,并连接数据磁盘。如果var.azure_db_server_create_orahome_diskfalse,则既不会创建托管磁盘,也不会连接数据磁盘。for_each块的使用有助于根据条件控制示例的数量。
我试图使用以下代码创建一个带有托管数据磁盘的Azure VM

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申请:


的数据
当我设置default = true时,将创建一个托管磁盘并将其附加到VM,如下所示。



设置default = false时,不会创建托管磁盘


相关问题