Azure上的Terraform公共ip输出

Azure上的Terraform公共ip输出,azure,terraform,Azure,Terraform,我按照示例[1]输出在Azure上使用Terraform创建的新VM的公共IP。当只创建1个VM时,它可以正常工作,但当我添加一个计数器(默认值为2)时,它不会输出任何内容 以下是我修改.tf文件的方式: variable "count" { default = "2" } ... resource "azurerm_public_ip" "test" { name = "test-pip" location

我按照示例[1]输出在Azure上使用Terraform创建的新VM的公共IP。当只创建1个VM时,它可以正常工作,但当我添加一个计数器(默认值为2)时,它不会输出任何内容

以下是我修改.tf文件的方式:

variable "count" {
    default = "2"
}
...

resource "azurerm_public_ip" "test" {
  name                         = "test-pip"
  location                     = "${azurerm_resource_group.test.location}"
  resource_group_name          = "${azurerm_resource_group.test.name}"
  public_ip_address_allocation = "Dynamic"
  idle_timeout_in_minutes      = 30

  tags {
    environment = "test"
  }
}
...

data "azurerm_public_ip" "test" {
  count               = "${var.count}"
  name                = "${element(azurerm_public_ip.test.*.name, count.index)}"
  resource_group_name = "${azurerm_virtual_machine.test.resource_group_name}"
}

output "public_ip_address" {
  value = "${data.azurerm_public_ip.test.*.ip_address}"
}
地形应用后:

Outputs:

public_ip_address = [
    ,

]

[1]

无法输出多公用IP的原因是您没有创建多公用IP。因此,当您使用
${data.azurerm\u public\u ip.test.*.ip\u address}
来输出它们时,您就没有这些资源了

对于terraform,您可以在资源
azurerm\u public\u ip
中添加
count
以创建多个公共ip,并将它们与
azurerm\u public\u ip.test.*.ip地址一起输出,如下所示:

variable "count" {
    default = "2"
}
...

resource "azurerm_public_ip" "test" {
  count                        = "${var.count}"
  name                         = "test-${count.index}-pip"
  location                     = "${azurerm_resource_group.test.location}"
  resource_group_name          = "${azurerm_resource_group.test.name}"
  public_ip_address_allocation = "Static"
  idle_timeout_in_minutes      = 30

  tags {
    environment = "test-${count.index}"
  }
}
...

output "public_ip_address" {
  value = "${azurerm_public_ip.test.*.ip_address}"
}
data "azurerm_public_ip" "test" {
  count               = "${var.count}"
  name                = "${element(azurerm_public_ip.test.*.name, count.index)}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

output "public_ip_address" {
  value = "${data.azurerm_public_ip.test.*.ip_address}"
}
结果的屏幕截图如下所示:

variable "count" {
    default = "2"
}
...

resource "azurerm_public_ip" "test" {
  count                        = "${var.count}"
  name                         = "test-${count.index}-pip"
  location                     = "${azurerm_resource_group.test.location}"
  resource_group_name          = "${azurerm_resource_group.test.name}"
  public_ip_address_allocation = "Static"
  idle_timeout_in_minutes      = 30

  tags {
    environment = "test-${count.index}"
  }
}
...

output "public_ip_address" {
  value = "${azurerm_public_ip.test.*.ip_address}"
}
data "azurerm_public_ip" "test" {
  count               = "${var.count}"
  name                = "${element(azurerm_public_ip.test.*.name, count.index)}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

output "public_ip_address" {
  value = "${data.azurerm_public_ip.test.*.ip_address}"
}

我做了测试,只是创建了公众。因此,我将分配方法更改为静态,并将其与资源一起输出

如果要使用
数据
引用公共IP。代码如下所示:

variable "count" {
    default = "2"
}
...

resource "azurerm_public_ip" "test" {
  count                        = "${var.count}"
  name                         = "test-${count.index}-pip"
  location                     = "${azurerm_resource_group.test.location}"
  resource_group_name          = "${azurerm_resource_group.test.name}"
  public_ip_address_allocation = "Static"
  idle_timeout_in_minutes      = 30

  tags {
    environment = "test-${count.index}"
  }
}
...

output "public_ip_address" {
  value = "${azurerm_public_ip.test.*.ip_address}"
}
data "azurerm_public_ip" "test" {
  count               = "${var.count}"
  name                = "${element(azurerm_public_ip.test.*.name, count.index)}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}

output "public_ip_address" {
  value = "${data.azurerm_public_ip.test.*.ip_address}"
}

希望这对你有帮助。如果您需要更多帮助,请告诉我。

因此,我在部署到Azure时遇到了完全相同的问题。 上述解决方案有效(Charles Xu的解决方案),但有一个警告。。。我必须: 硬编码资源组的名称, 以及向输出块添加一个依赖子句

我确信上面的答案是正确的,但是数据对象“azurerm_public_ip”中资源组键的值需要硬编码


我实际上在公共ip中有count变量,但我从错误的位置复制粘贴了它,对不起。在任何情况下,将分配从动态更改为静态都可以使其工作,并且我可以在输出中看到2个IP。谢谢!我将分配从动态更改为静态,只是因为我需要正确显示地址,并且我只创建公共IP进行测试,没有虚拟机。不管怎样,如果答案有帮助,你可以接受。为什么要投否决票?我已经回答了这个问题,并提供了我的理解(附图片)