Terraform - Azure - 有条件地在可用性集中创建 VM

Terraform - Azure - Create VM in availability set conditionally

尝试在 Terraform 中使用和不使用可用性集创建 VM。这个想法是使用一个模板,如果没有提供可用性集名称,它默认为空,那么 VM 将不会添加到可用性集中。我确实尝试过使用 "count",如 'count = var.avail_set != "" ? 1 : 0',但是即使我有两个有条件执行的部分,它也没有完全按照我想要的方式工作,我需要 VM 资源的名称相同所以我可以稍后在代码中添加日志分析和备份。 .请在下面查看我的代码:

    name = "${var.resource_group_name}"
}
data azurerm_subnet sndata02 {
    name = "${var.subnet_name}"
  resource_group_name = "${var.core_resource_group_name}"
  virtual_network_name = "${var.virtual_network_name}"
}
data azurerm_availability_set availsetdata02 {
  name = "${var.availability_set_name}"
  resource_group_name = "${var.resource_group_name}"
}
data azurerm_backup_policy_vm bkpoldata02 {
  name                = "${var.backup_policy_name}"
  recovery_vault_name = "${var.recovery_services_vault_name}"
  resource_group_name = "${var.core_resource_group_name}"
}
data azurerm_log_analytics_workspace law02 { 
  name                = "${var.log_analytics_workspace_name}"
  resource_group_name = "${var.core_resource_group_name}"
}
#===================================================================
# Create NIC
#===================================================================
resource "azurerm_network_interface" "vmnic02" {
  name                = "nic${var.virtual_machine_name}"
  location            = "${data.azurerm_resource_group.rgdata02.location}"
  resource_group_name = "${var.resource_group_name}"

  ip_configuration {
    name                          = "ipcnfg${var.virtual_machine_name}"
    subnet_id                     = "${data.azurerm_subnet.sndata02.id}"
    private_ip_address_allocation = "Static"
    private_ip_address            = "${var.private_ip}"
  }
}
#===================================================================
# Create VM with Availability Set
#===================================================================
resource "azurerm_virtual_machine" "vm02" {
  count = var.avail_set != "" ? 1 : 0
  depends_on            = [azurerm_network_interface.vmnic02]
  name                  = "${var.virtual_machine_name}"
  location              = "${data.azurerm_resource_group.rgdata02.location}"
  resource_group_name   = "${var.resource_group_name}"
  network_interface_ids = [azurerm_network_interface.vmnic02.id]
  vm_size               = "${var.virtual_machine_size}"
  availability_set_id   = "${data.azurerm_availability_set.availsetdata02.id}"
  tags                  = var.tags

  # This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
  # NOTE: This may not be optimal in all cases.
  delete_os_disk_on_termination = true

  os_profile {
    computer_name  = "${var.virtual_machine_name}"
    admin_username = "__VMUSER__"
    admin_password = "__VMPWD__"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  storage_image_reference {
    id = "${var.image_id}"
  }

  storage_os_disk {
    name              = "${var.virtual_machine_name}osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
    os_type           = "Linux"     
  }

  boot_diagnostics {
    enabled = true
    storage_uri = "${var.boot_diagnostics_uri}"
  } 
}
#===================================================================
# Create VM without Availability Set
#===================================================================
resource "azurerm_virtual_machine" "vm03" {
  count = var.avail_set == "" ? 1 : 0
  depends_on            = [azurerm_network_interface.vmnic02]
  name                  = "${var.virtual_machine_name}"
  location              = "${data.azurerm_resource_group.rgdata02.location}"
  resource_group_name   = "${var.resource_group_name}"
  network_interface_ids = [azurerm_network_interface.vmnic02.id]
  vm_size               = "${var.virtual_machine_size}"
  # availability_set_id   = "${data.azurerm_availability_set.availsetdata02.id}"
  tags                  = var.tags

  # This means the OS Disk will be deleted when Terraform destroys the Virtual Machine
  # NOTE: This may not be optimal in all cases.
  delete_os_disk_on_termination = true

  os_profile {
    computer_name  = "${var.virtual_machine_name}"
    admin_username = "__VMUSER__"
    admin_password = "__VMPWD__"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  storage_image_reference {
    id = "${var.image_id}"
  }

  storage_os_disk {
    name              = "${var.virtual_machine_name}osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
    os_type           = "Linux"     
  }

  boot_diagnostics {
    enabled = true
    storage_uri = "${var.boot_diagnostics_uri}"
  } 
}
#===================================================================
# Set Monitoring and Log Analytics Workspace
#===================================================================
resource "azurerm_virtual_machine_extension" "oms_mma02" {
  count = var.bootstrap ? 1 : 0
  name                       = "${var.virtual_machine_name}-OMSExtension"
  virtual_machine_id         = "${azurerm_virtual_machine.vm02.id}"
  publisher                  = "Microsoft.EnterpriseCloud.Monitoring"
  type                       = "OmsAgentForLinux"
  type_handler_version       = "1.8"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
      "workspaceId" : "${data.azurerm_log_analytics_workspace.law02.workspace_id}"
    }
  SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
    {
      "workspaceKey" : "${data.azurerm_log_analytics_workspace.law02.primary_shared_key}"
    }
  PROTECTED_SETTINGS
}
#===================================================================
# Associate VM to Backup Policy
#===================================================================
resource "azurerm_backup_protected_vm" "vm02" {
  count = var.bootstrap ? 1 : 0
  resource_group_name = "${var.core_resource_group_name}"
  recovery_vault_name = "${var.recovery_services_vault_name}"
  source_vm_id        = "${azurerm_virtual_machine.vm02.id}"
  backup_policy_id    = "${data.azurerm_backup_policy_vm.bkpoldata02.id}"
}

count属性只控制资源的个数,对你来说就是创建或不创建VM,不会改变VM的配置。这不是适合您情况的正确方法。

正如我所想,您可以像这样使用 VM 属性 availability_set_id 的条件表达式:

availability_set_id = var.avail_set != "" ? "${data.azurerm_availability_set.availsetdata02.id}" : ""