我正在尝试为集群和单节点部署创建虚拟机。
resource "azurerm_virtual_machine" "app" {
name = "${var.name_prefix}-${format("%02d", count.index+1)}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.resourcegroup.name}"
availability_set_id = "${azurerm_availability_set.avset.id}"
network_interface_ids = ["${element(azurerm_network_interface.networkinterface-app.*.id, count.index+1)}"]
vm_size = "${var.vm_size}"
count = "${var.app_nodes}"
该设置对于配置了 availability_set_id
的集群节点部署效果很好,但会失败,因为在单节点中我不需要配置 availability_set_id
。
我当前使用 Terraform v0.11.2
和 *provider.azurerm: version = "~> 1.0"
作为 terraform 和 azurerm 提供程序的版本。
任何人都可以帮助我如何在这种情况下使用条件来处理这两种情况吗?
最佳答案
我认为您需要将其包装在一个模块中,然后拥有两个资源并使用 count = "${var.somecond ? 1 : 0}"
请确定哪一个处于事件状态。
关于azure - 如何使 terraform 资源中的字段成为可选字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48306089/