简体   繁体   English

Azure上的Terraform公共IP输出

[英]Terraform public ip output on Azure

I am following the example [1] to output the public IP of a new VM created on Azure with Terraform. 我正在按照示例[1]输出在Azure上使用Terraform创建的新VM的公共IP。 It works fine when creating only 1 VM, but when I add a counter (default 2), it doesn't output anything. 仅创建1个VM时,它工作正常,但是当我添加一个计数器(默认值为2)时,它不输出任何内容。

This is how I am modifying the .tf file: 这就是我修改.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}"
}

After terraform apply: 地形应用后:

Outputs:

public_ip_address = [
    ,

]

[1] https://www.terraform.io/docs/providers/azurerm/d/public_ip.html [1] https://www.terraform.io/docs/providers/azurerm/d/public_ip.html

The reason that you cannot output the multi public IPs is that you do not create multi public IPs. 无法输出多个公共IP的原因是,您没有创建多个公共IP。 So when you use ${data.azurerm_public_ip.test.*.ip_address} to output them, there no these resources for you. 因此,当您使用${data.azurerm_public_ip.test.*.ip_address}输出它们时,没有这些资源可供您使用。

For the terraform, you could add the count in the resource azurerm_public_ip to create multi public IPs and output them with azurerm_public_ip.test.*.ip_address like this: 对于terraform,可以将count添加到资源azurerm_public_ip以创建多个公共IP,并使用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}"
}

The screenshot of the result like this: 结果的屏幕截图如下:

在此处输入图片说明

I did the test just create the public. 我做的测试只是创造了公众。 So I change the allocation method into Static and output it with the resource. 因此,我将分配方法更改为静态,并与资源一起输出。

If you want to use the data to reference the public IPs. 如果要使用data引用公共IP。 The code would like this: 代码如下:

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}"
}

Hope this will help you. 希望这会帮助你。 If you need more help please let me know. 如果您需要更多帮助,请告诉我。

So I just ran into the exact same problem with my deployment into Azure. 因此,我在部署到Azure时遇到了完全相同的问题。 The above solutions worked( Charles Xu's solution), with one caveat... I had to: hard-code the name of the resource group , as well as add a depends on clause to the output block. 上面的解决方案(Charles Xu的解决方案)起作用了,但有一个警告……我必须: 硬编码资源组的名称 ,以及在输出块中添加一个depends子句

I am sure that the above answers are the correct way to go about it however the value of the resource group key in the data object "azurerm_public_ip" needed to be hardcoded... 我确定上述答案是正确的解决方法,但是数据对象“ azurerm_public_ip”中资源组键的值需要硬编码。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM