无法通过terraform将动态IP设置为Azure虚拟机
不确定哪里出了问题,但我无法让下面的代码正常工作 目标:使用公共IP创建两个(或更多)虚拟机 问题:如错误代码块所示,地面计划报告错误 地形代码块位于下方:无法通过terraform将动态IP设置为Azure虚拟机,terraform,azure-virtual-machine,terraform-provider-azure,Terraform,Azure Virtual Machine,Terraform Provider Azure,不确定哪里出了问题,但我无法让下面的代码正常工作 目标:使用公共IP创建两个(或更多)虚拟机 问题:如错误代码块所示,地面计划报告错误 地形代码块位于下方: resource "azurerm_public_ip" "tf-pubip-cluster-aos" { count = 2 name = "${var.ax_base_hostname}-${count.index+1}-PUB
resource "azurerm_public_ip" "tf-pubip-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-PUBIP"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
allocation_method = "Dynamic"
}
resource "azurerm_network_interface" "tf-ni-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-NI"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
ip_configuration {
name = "${var.ax_base_hostname}-${count.index+1}-IP"
subnet_id = "${data.azurerm_subnet.tf-sn-cluster-aos.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.tf-pubip-cluster-aos.id}"
}
}
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
availability_set_id = "${azurerm_availability_set.tf-as-cluster-aos.id}"
network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index)}"]
vm_size = "${var.ax_vm_size}"
Error running plan: 1 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos: 2 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos[0]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
azurerm_network_interface.tf-ni-cluster-aos[1]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
}
错误消息如下所示:
resource "azurerm_public_ip" "tf-pubip-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-PUBIP"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
allocation_method = "Dynamic"
}
resource "azurerm_network_interface" "tf-ni-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-NI"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
ip_configuration {
name = "${var.ax_base_hostname}-${count.index+1}-IP"
subnet_id = "${data.azurerm_subnet.tf-sn-cluster-aos.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.tf-pubip-cluster-aos.id}"
}
}
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
availability_set_id = "${azurerm_availability_set.tf-as-cluster-aos.id}"
network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index)}"]
vm_size = "${var.ax_vm_size}"
Error running plan: 1 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos: 2 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos[0]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
azurerm_network_interface.tf-ni-cluster-aos[1]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
我想不出来。。。任何帮助都会很好。您创建了两个公共ip,而不是一个,但您尝试引用它,就像它是一个单一的ip,但它不是。这是一张单子。您需要获取个人的公共ip id,如下所示:
"${element(azurerm_public_ip.tf-pubip-cluster-aos.*.id, count.index)}"
谢谢@4c74356b41:)这是我的错误,我以前用过它,但我不知道它真正的作用是什么?请允许我问一下,.*.id,count.index实际上是做什么的?从id的列表中获取元素编号x。您可以阅读关于元素function*的文档表达式“resource_name.*.id”中的*称为“splat”运算符。它返回一个特定“resource_name”类型创建的所有资源的列表(数组),无需事先定义列表变量。然后,与任何其他列表一样,您可以使用“element(,)”函数访问列表中的任何元素,该函数接收2个参数1。清单2。索引(列表中编号的位置)