Ari*_*nga 9 azure azure-virtual-machine terraform terraform-provider-azure azure-public-ip
我正在 azurerm_virtual_machine 中通过计数创建 4 个虚拟机,但我只想创建一个公共 IP 并将其与第一个虚拟机关联?这可能吗?如果可以的话怎么办?
下面是我的模板文件
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
}
}
resource "azurerm_public_ip" "public_ip" {
name = ...
location = ...
resource_group_name = ...
}
resource "azurerm_virtual_machine" "vms" {
count = 4
network_interface_ids = [element(azurerm_network_interface.nics.*.id, count.index)]
}
Run Code Online (Sandbox Code Playgroud)
我已经解决了以下问题,但它们创建了多个公共 IP 并将它们添加到所有虚拟机中。
Mar*_*cin 11
公共 IP 是使用azurerm_public_ip创建的:
resource "azurerm_public_ip" "public_ip" {
name = "acceptanceTestPublicIp1"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
Run Code Online (Sandbox Code Playgroud)
有了地址,您可以使用条件表达式azurerm_network_interface执行以下操作:
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
public_ip_address_id = count.index == 1 ? azurerm_public_ip.public_ip.id : null
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11661 次 |
| 最近记录: |