英文:
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 "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
</details>
# 答案1
**得分**: 0
问题已通过创建动态的 "storage_data_disk" {} 并在其中使用 for_each 解决。
<details>
<summary>英文:</summary>
Issue has been resloved by making dynamic "storage_data_disk" {} 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 "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:
When I set default = true
, a managed disk is created and attached to the VM as below.
When I set default = false
, a managed disk is not created
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论