基于变量的Terraform Azure磁盘附加

huangapple go评论74阅读模式
英文:

Terraform Azure disk attachment based on a variable

问题

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.

```hcl
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 happening properly and fine. Please correct if this method also not correct.

Problem is, while VM creation, it's still trying to attach that disk and find a managed_disk_id even if 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 disks 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 requirement.


<details>
<summary>英文:</summary>

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 &quot;azurerm_managed_disk&quot; &quot;dbhome&quot; {
count = var.azure_db_server_create_orahome_disk ? 1 : 0
name = &quot;${local.db_server_name}-orahome-0&quot;
location = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
storage_account_type = &quot;Premium_LRS&quot;
create_option = &quot;Empty&quot;
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            = &quot;${local.db_server_name}-orahome-0&quot;
    caching         = &quot;ReadOnly&quot;
    managed_disk_id = azurerm_managed_disk.dbhome.id
    create_option   = &quot;Attach&quot;
    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

</details>


# 答案1
**得分**: 0

问题已通过创建动态的 "storage_data_disk" {} 并在其中使用 for_each 解决。

<details>
<summary>英文:</summary>

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

</details>



# 答案2
**得分**: 0

需要控制只有在`var.azure_db_server_create_orahome_disk`为`true`时才创建`orahome`资源,当它为`false`时不应创建。通过使用`for_each`,我们可以解决相同的问题。

下面是用于创建具有托管数据磁盘的Azure VM 的代码示例:

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

当将default设置为true时,将创建一个托管磁盘并附加到虚拟机,当将default设置为false时,不会创建托管磁盘。

英文:

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

Terraform Apply:

基于变量的Terraform Azure磁盘附加

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

基于变量的Terraform Azure磁盘附加

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

基于变量的Terraform Azure磁盘附加

huangapple
  • 本文由 发表于 2023年8月4日 02:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定