Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

220 рядки
9.3 KiB

  1. terraform {
  2. required_version = "~> 0.12.21"
  3. required_providers {
  4. azurerm = "~> 1.44"
  5. }
  6. }
  7. resource "azurerm_virtual_machine" "azvm" {
  8. count = length(var.vms)
  9. name = var.vms[count.index].name
  10. resource_group_name = var.vms[count.index].resource_group_name
  11. location = var.vms[count.index].location
  12. network_interface_ids = [for nic in var.vms[count.index].nics: azurerm_network_interface.nic[join("-nic", [var.vms[count.index].name, index(var.vms[count.index].nics, nic)])].id]
  13. vm_size = var.vms[count.index].vm_size #Standard_D2s_v3, Standard_B2s
  14. availability_set_id = azurerm_availability_set.avset[var.vms[count.index].avset.name].id
  15. tags = var.vms[count.index].tags
  16. delete_os_disk_on_termination = try(var.vms[count.index].delete_os_disk_on_termination, true)
  17. delete_data_disks_on_termination = try(var.vms[count.index].delete_data_disks_on_termination, true)
  18. #zones = try(var.vms[count.index].zones, null)
  19. storage_os_disk {
  20. name = join("-", [var.vms[count.index].name, "disk"])
  21. caching = var.vms[count.index].storage_os_disk.caching #None, ReadOnly or ReadWrite
  22. create_option = var.vms[count.index].storage_os_disk.create_option #Attach, FromImage
  23. disk_size_gb = var.vms[count.index].storage_os_disk.disk_size_gb
  24. os_type = var.vms[count.index].storage_os_disk.os_type #Linux or Windows
  25. }
  26. dynamic "os_profile" {
  27. for_each = var.vms[count.index].os_profile
  28. content {
  29. computer_name = var.vms[count.index].name
  30. admin_username = try(os_profile.value.admin_username, "") != "" ? os_profile.value.admin_username : "master"
  31. admin_password = try(os_profile.value.admin_password, null)
  32. }
  33. }
  34. dynamic "os_profile_linux_config" {
  35. for_each = try(var.vms[count.index].os_profile_linux_config, [])
  36. content {
  37. disable_password_authentication = os_profile_linux_config.value.disable_password_authentication
  38. dynamic "ssh_keys" {
  39. for_each = os_profile_linux_config.value.disable_password_authentication ? os_profile_linux_config.value.ssh_keys : {}
  40. content {
  41. key_data = try(ssh_keys.value.file, "") != "" ? file("${ssh_keys.value.file}") : var.default_ssh_pubkey
  42. path = try(ssh_keys.value.dst_path, "") != "" ? ssh_keys.value.dst_path : format("/home/%s/.ssh/authorized_keys", "master")
  43. }
  44. }
  45. }
  46. }
  47. dynamic "os_profile_windows_config" {
  48. for_each = try(var.vms[count.index].os_profile_windows_config, [])
  49. content {
  50. provision_vm_agent = os_profile_windows_config.value.provision_vm_agent
  51. enable_automatic_upgrades = os_profile_windows_config.value.enable_automatic_upgrades
  52. timezone = os_profile_windows_config.value.timezone #https://jackstromberg.com/2017/01/list-of-time-zones-consumed-by-azure/
  53. winrm {
  54. protocol = os_profile_windows_config.value.protocol #HTTP or HTTPS
  55. certificate_url = os_profile_windows_config.value.certificate_url
  56. }
  57. }
  58. }
  59. dynamic "plan" {
  60. for_each = try(var.vms[count.index].plan, [])
  61. content {
  62. name = plan.value.name
  63. publisher = plan.value.publisher
  64. product = plan.value.product
  65. }
  66. }
  67. dynamic "storage_data_disk" {
  68. for_each = try(var.vms[count.index].storage_data_disk, [])
  69. content {
  70. name = storage_data_disk.value.name
  71. caching = try(storage_data_disk.value.caching, "ReadOnly") #None, ReadOnly or ReadWrite
  72. create_option = try(storage_data_disk.value.create_option, "Empty") #Attach, FromImage and Empty
  73. disk_size_gb = storage_data_disk.value.disk_size_gb
  74. lun = storage_data_disk.value.lun
  75. }
  76. }
  77. dynamic "storage_image_reference" {
  78. for_each = var.vms[count.index].storage_image_reference
  79. content {
  80. publisher = storage_image_reference.value.publisher
  81. offer = storage_image_reference.value.offer
  82. sku = storage_image_reference.value.sku
  83. version = storage_image_reference.value.version
  84. id = try(storage_image_reference.value.id, null)
  85. }
  86. }
  87. depends_on = [azurerm_marketplace_agreement.accept, var.azvm_depends_on]
  88. }
  89. locals {
  90. # flatten ensures that this local value is a flat list of objects, rather
  91. # than a list of lists of objects.
  92. avsets = flatten([
  93. for vm in var.vms : {
  94. name = vm.avset.name
  95. location = vm.location
  96. resource_group_name = try(vm.avset.resource_group_name, vm.resource_group_name)
  97. platform_update_domain_count = try(vm.avset.platform_update_domain_count, 2)
  98. platform_fault_domain_count = try(vm.avset.platform_fault_domain_count, 2)
  99. managed = try(vm.avset.managed, true) #true => Aligned, false => Classic
  100. tags = try(vm.avset.tags, vm.tags)
  101. }
  102. ])
  103. }
  104. resource "azurerm_availability_set" "avset" {
  105. for_each = {
  106. for avs in local.avsets:
  107. avs.name => avs...
  108. }
  109. name = each.value[0].name
  110. location = each.value[0].location
  111. resource_group_name = each.value[0].resource_group_name
  112. platform_update_domain_count = each.value[0].platform_update_domain_count
  113. platform_fault_domain_count = each.value[0].platform_fault_domain_count
  114. managed = each.value[0].managed
  115. tags = each.value[0].tags
  116. }
  117. locals {
  118. # flatten ensures that this local value is a flat list of objects, rather
  119. # than a list of lists of objects.
  120. images = flatten([
  121. for vm in var.vms : [
  122. for image in vm.storage_image_reference : {
  123. name = join("-", [image.publisher, image.offer, image.sku])
  124. publisher = image.publisher
  125. offer = image.offer
  126. plan = image.sku
  127. }
  128. ]
  129. ])
  130. }
  131. resource "azurerm_marketplace_agreement" "accept" {
  132. for_each = {
  133. for image in local.images:
  134. image.name => image...
  135. }
  136. publisher = each.value[0].publisher
  137. offer = each.value[0].offer
  138. plan = each.value[0].plan
  139. }
  140. locals {
  141. # flatten ensures that this local value is a flat list of objects, rather
  142. # than a list of lists of objects.
  143. nics = flatten([
  144. for vm in var.vms : [
  145. for nic in vm.nics : {
  146. name = join("-nic", [vm.name, index(vm.nics, nic)])
  147. location = vm.location
  148. resource_group_name = nic.resource_group_name
  149. enable_ip_forwarding = try(nic.enable_ip_forwarding, false)
  150. enable_accelerated_networking = try(nic.enable_accelerated_networking, false)
  151. tags = nic.tags
  152. nsg = nic.nsg
  153. subnet = nic.subnet
  154. ip_configuration = {
  155. "name" = "${join("-nic", [vm.name, index(vm.nics, nic)])}-ipc"
  156. "private_ip_address_allocation" = try(nic.ip_configuration.private_ip_address_allocation, "Static")
  157. "private_ip_address_version" = try(nic.ip_configuration.private_ip_address_version, "IPv4")
  158. "private_ip_address" = try(nic.ip_configuration.private_ip_address_allocation, "Static") == "Static" ? nic.ip_configuration.private_ip_address : null
  159. "public_ip_address_id" = try(nic.ip_configuration.public_ip_address_id, null)
  160. }
  161. }
  162. ]
  163. ])
  164. }
  165. resource "azurerm_network_interface" "nic" {
  166. for_each = {for nic in local.nics: nic.name => nic}
  167. name = each.value.name
  168. location = each.value.location
  169. resource_group_name = each.value.resource_group_name
  170. enable_ip_forwarding = each.value.enable_ip_forwarding
  171. enable_accelerated_networking = each.value.enable_accelerated_networking
  172. network_security_group_id = data.azurerm_network_security_group.nsg[each.value.name].id
  173. tags = each.value.tags
  174. ip_configuration {
  175. name = each.value.ip_configuration.name
  176. subnet_id = data.azurerm_subnet.sub[each.value.name].id
  177. private_ip_address_allocation = each.value.ip_configuration.private_ip_address_allocation
  178. private_ip_address_version = each.value.ip_configuration.private_ip_address_version
  179. private_ip_address = try(each.value.ip_configuration.private_ip_address, null)
  180. public_ip_address_id = try(each.value.ip_configuration.public_ip_address_id, null)
  181. }
  182. }
  183. data "azurerm_network_security_group" "nsg" {
  184. for_each = {for nic in local.nics: nic.name => nic}
  185. name = each.value.nsg.name
  186. resource_group_name = each.value.nsg.resource_group_name
  187. depends_on = [var.azvm_depends_on]
  188. }
  189. data "azurerm_subnet" "sub" {
  190. for_each = {for nic in local.nics: nic.name => nic}
  191. name = each.value.subnet.name
  192. virtual_network_name = each.value.subnet.virtual_network_name
  193. resource_group_name = each.value.subnet.resource_group_name
  194. depends_on = [var.azvm_depends_on]
  195. }