我按照示例[1]输出使用Terraform在Azure上创建的新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] https://www.terraform.io/docs/providers/azurerm/d/public_ip.html
答案 0 :(得分:0)
之所以不能输出多个公共IP,是因为您没有创建多个公共IP。因此,当您使用${data.azurerm_public_ip.test.*.ip_address}
输出它们时,没有适合您的这些资源。
对于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}"
}
结果的屏幕截图如下:
我所做的测试只是建立了公众。因此,我将分配方法更改为静态,并与资源一起输出。
如果要使用data
引用公共IP。代码如下:
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}"
}
希望这会对您有所帮助。如果您需要更多帮助,请告诉我。
答案 1 :(得分:-1)