基于变量的Terraform Azure磁盘附加

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

Terraform Azure disk attachment based on a variable

问题

  1. 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.
  2. ```hcl
  3. resource "azurerm_managed_disk" "dbhome" {
  4. count = var.azure_db_server_create_orahome_disk ? 1 : 0
  5. name = "${local.db_server_name}-orahome-0"
  6. location = data.azurerm_resource_group.rg.location
  7. resource_group_name = data.azurerm_resource_group.rg.name
  8. storage_account_type = "Premium_LRS"
  9. create_option = "Empty"
  10. disk_size_gb = var.dbdisksize_dbhome
  11. zones = local.availability_zone
  12. }

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.

  1. resource "azurerm_virtual_machine" "oracle19c" {
  2. name = local.db_server_name
  3. location = azurerm_network_interface.nic.location
  4. resource_group_name = data.azurerm_resource_group.rg.name
  5. network_interface_ids = [azurerm_network_interface.nic.id]
  6. vm_size = contains(var.vm_nonsupported_regions, var.region) ? var.dbvmsize_fallback : var.dbvmsize
  7. delete_os_disk_on_termination = true
  8. delete_data_disks_on_termination = true
  9. storage_data_disk {
  10. count = var.azure_db_server_create_orahome_disk ? 1 : 0
  11. name = "${local.db_server_name}-orahome-0"
  12. caching = "ReadOnly"
  13. managed_disk_id = azurerm_managed_disk.dbhome.id
  14. create_option = "Attach"
  15. lun = 1
  16. disk_size_gb = var.dbdisksize_dbhome
  17. }
  18. }

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.

  1. <details>
  2. <summary>英文:</summary>
  3. 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
}

  1. 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.
  2. 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

  1. storage_data_disk {
  2. count = var.azure_db_server_create_orahome_disk ? 1 : 0
  3. name = &quot;${local.db_server_name}-orahome-0&quot;
  4. caching = &quot;ReadOnly&quot;
  5. managed_disk_id = azurerm_managed_disk.dbhome.id
  6. create_option = &quot;Attach&quot;
  7. lun = 1
  8. disk_size_gb = var.dbdisksize_dbhome
  9. }
  10. }`
  1. 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. </details>
  3. # 答案1
  4. **得分**: 0
  5. 问题已通过创建动态的 "storage_data_disk" {} 并在其中使用 for_each 解决。
  6. <details>
  7. <summary>英文:</summary>
  8. Issue has been resloved by making dynamic &quot;storage_data_disk&quot; {} and also with for_each inside that.
  9. </details>
  10. # 答案2
  11. **得分**: 0
  12. 需要控制只有在`var.azure_db_server_create_orahome_disk``true`时才创建`orahome`资源,当它为`false`时不应创建。通过使用`for_each`,我们可以解决相同的问题。
  13. 下面是用于创建具有托管数据磁盘的Azure VM 的代码示例:
  14. ```hcl
  15. provider "azurerm" {
  16. features {}
  17. }
  18. variable "azure_db_server_create_orahome_disk" {
  19. type = bool
  20. default = true
  21. }
  22. variable "dbdisksize_dbhome" {
  23. type = number
  24. default = 128
  25. }
  26. variable "dbvmsize" {
  27. type = string
  28. default = "Standard_DS2_v2"
  29. }
  30. variable "dbvmsize_fallback" {
  31. type = string
  32. default = "Standard_DS1_v2"
  33. }
  34. locals {
  35. db_server_name = "my-db-server"
  36. availability_zone = ["1"]
  37. }
  38. resource "azurerm_resource_group" "example" {
  39. name = "vm-resources"
  40. location = "East US"
  41. }
  42. resource "azurerm_virtual_network" "main" {
  43. name = "vm-network"
  44. address_space = ["10.0.0.0/16"]
  45. location = azurerm_resource_group.example.location
  46. resource_group_name = azurerm_resource_group.example.name
  47. depends_on = [azurerm_resource_group.example]
  48. }
  49. resource "azurerm_subnet" "internal" {
  50. name = "internal"
  51. resource_group_name = azurerm_resource_group.example.name
  52. virtual_network_name = azurerm_virtual_network.main.name
  53. address_prefixes = ["10.0.2.0/24"]
  54. depends_on = [azurerm_virtual_network.main]
  55. }
  56. resource "azurerm_network_interface" "main" {
  57. name = "vm-nic"
  58. location = azurerm_resource_group.example.location
  59. resource_group_name = azurerm_resource_group.example.name
  60. ip_configuration {
  61. name = "testconfiguration1"
  62. subnet_id = azurerm_subnet.internal.id
  63. private_ip_address_allocation = "Dynamic"
  64. }
  65. depends_on = [azurerm_subnet.internal]
  66. }
  67. resource "azurerm_managed_disk" "dbhome" {
  68. count = var.azure_db_server_create_orahome_disk ? 1 : 0
  69. name = "${local.db_server_name}-orahome-0"
  70. location = azurerm_resource_group.example.location
  71. resource_group_name = azurerm_resource_group.example.name
  72. storage_account_type = "Premium_LRS"
  73. create_option = "Empty"
  74. disk_size_gb = var.dbdisksize_dbhome
  75. depends_on = [azurerm_network_interface.main]
  76. }
  77. resource "azurerm_virtual_machine" "oracle19c" {
  78. name = "venkatvm1"
  79. location = azurerm_resource_group.example.location
  80. resource_group_name = azurerm_resource_group.example.name
  81. network_interface_ids = [azurerm_network_interface.main.id]
  82. vm_size = "Standard_D8s_v3"
  83. delete_os_disk_on_termination = true
  84. delete_data_disks_on_termination = true
  85. storage_os_disk {
  86. name = "vm-os-disk"
  87. caching = "ReadWrite"
  88. managed_disk_type = "Standard_LRS"
  89. create_option = "FromImage"
  90. disk_size_gb = 64
  91. }
  92. storage_image_reference {
  93. publisher = "SUSE"
  94. offer = "sles-sap-12-sp5"
  95. sku = "gen1"
  96. version = "latest"
  97. }
  98. dynamic "storage_data_disk" {
  99. for_each = azurerm_managed_disk.dbhome
  100. content {
  101. name = "${local.db_server_name}-orahome-0"
  102. caching = "None"
  103. managed_disk_id = try(storage_data_disk.value.id, null)
  104. create_option = "Attach"
  105. lun = 1
  106. disk_size_gb = var.dbdisksize_dbhome
  107. }
  108. }
  109. os_profile {
  110. computer_name = "venakt-vm"
  111. admin_username = "Admin"
  112. admin_password = "Pa$$word@123$"
  113. }
  114. os_profile_linux_config {
  115. disable_password_authentication = false
  116. }
  117. }

当将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.

  1. provider &quot;azurerm&quot; {
  2. features {}
  3. }
  4. variable &quot;azure_db_server_create_orahome_disk&quot; {
  5. type = bool
  6. default = true
  7. }
  8. variable &quot;dbdisksize_dbhome&quot; {
  9. type = number
  10. default = 128
  11. }
  12. variable &quot;dbvmsize&quot; {
  13. type = string
  14. default = &quot;Standard_DS2_v2&quot;
  15. }
  16. variable &quot;dbvmsize_fallback&quot; {
  17. type = string
  18. default = &quot;Standard_DS1_v2&quot;
  19. }
  20. locals {
  21. db_server_name = &quot;my-db-server&quot;
  22. availability_zone = [&quot;1&quot;]
  23. }
  24. resource &quot;azurerm_resource_group&quot; &quot;example&quot; {
  25. name = &quot;vm-resources&quot;
  26. location = &quot;East US&quot;
  27. }
  28. resource &quot;azurerm_virtual_network&quot; &quot;main&quot; {
  29. name = &quot;vm-network&quot;
  30. address_space = [&quot;10.0.0.0/16&quot;]
  31. location = azurerm_resource_group.example.location
  32. resource_group_name = azurerm_resource_group.example.name
  33. depends_on = [ azurerm_resource_group.example ]
  34. }
  35. resource &quot;azurerm_subnet&quot; &quot;internal&quot; {
  36. name = &quot;internal&quot;
  37. resource_group_name = azurerm_resource_group.example.name
  38. virtual_network_name = azurerm_virtual_network.main.name
  39. address_prefixes = [&quot;10.0.2.0/24&quot;]
  40. depends_on = [ azurerm_virtual_network.main ]
  41. }
  42. resource &quot;azurerm_network_interface&quot; &quot;main&quot; {
  43. name = &quot;vm-nic&quot;
  44. location = azurerm_resource_group.example.location
  45. resource_group_name = azurerm_resource_group.example.name
  46. ip_configuration {
  47. name = &quot;testconfiguration1&quot;
  48. subnet_id = azurerm_subnet.internal.id
  49. private_ip_address_allocation = &quot;Dynamic&quot;
  50. }
  51. depends_on = [ azurerm_subnet.internal ]
  52. }
  53. resource &quot;azurerm_managed_disk&quot; &quot;dbhome&quot; {
  54. count = var.azure_db_server_create_orahome_disk ? 1 : 0
  55. name = &quot;${local.db_server_name}-orahome-0&quot;
  56. location = azurerm_resource_group.example.location
  57. resource_group_name = azurerm_resource_group.example.name
  58. storage_account_type = &quot;Premium_LRS&quot;
  59. create_option = &quot;Empty&quot;
  60. disk_size_gb = var.dbdisksize_dbhome
  61. depends_on = [ azurerm_network_interface.main ]
  62. }
  63. resource &quot;azurerm_virtual_machine&quot; &quot;oracle19c&quot; {
  64. name = &quot;venkatvm1&quot;
  65. location = azurerm_resource_group.example.location
  66. resource_group_name = azurerm_resource_group.example.name
  67. network_interface_ids = [azurerm_network_interface.main.id]
  68. vm_size = &quot;Standard_D8s_v3&quot;
  69. delete_os_disk_on_termination = true
  70. delete_data_disks_on_termination = true
  71. storage_os_disk {
  72. name = &quot;vm-os-disk&quot;
  73. caching = &quot;ReadWrite&quot;
  74. managed_disk_type = &quot;Standard_LRS&quot;
  75. create_option = &quot;FromImage&quot;
  76. disk_size_gb = 64
  77. }
  78. storage_image_reference {
  79. publisher = &quot;SUSE&quot;
  80. offer = &quot;sles-sap-12-sp5&quot;
  81. sku = &quot;gen1&quot;
  82. version = &quot;latest&quot;
  83. }
  84. dynamic &quot;storage_data_disk&quot; {
  85. for_each = azurerm_managed_disk.dbhome
  86. content {
  87. name = &quot;${local.db_server_name}-orahome-0&quot;
  88. caching = &quot;None&quot;
  89. managed_disk_id = try(storage_data_disk.value.id, null)
  90. create_option = &quot;Attach&quot;
  91. # managed_disk_type = &quot;&quot;
  92. lun = 1
  93. disk_size_gb = var.dbdisksize_dbhome
  94. }
  95. }
  96. os_profile {
  97. computer_name = &quot;venakt-vm&quot;
  98. admin_username = &quot;Admin&quot;
  99. admin_password = &quot;Pa$$word@123$&quot;
  100. }
  101. os_profile_linux_config {
  102. disable_password_authentication = false
  103. }
  104. }

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:

确定