Terraform:使用If/Else模式将资源属性分配给资源参数
我在Terraform中使用IF/ELSE pattern来构建带或不带公共IP的NIC。分配NIC时出现问题。我无法找到一种技术,可以让我选择用于创建NIC的资源(有或没有公共IP)。使用三元操作失败,因为其中一个资源不存在。将资源放在列表中不会内插。我如何分配正确的资源输出?Terraform:使用If/Else模式将资源属性分配给资源参数
resource "azurerm_public_ip" "public_ip" {
count = "${var.assign_public_ip}"
name = "${format("${var.name}-pip%02d", count.index)}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
public_ip_address_allocation = "static"
tags {
environment = "${var.resource_group_name}"
}
}
resource "azurerm_network_interface" "nic_with_public_ip" {
count = "${var.assign_public_ip}"
name = "${format("${var.name}-nic%02d", count.index)}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
ip_configuration {
name = "ip_cfg"
subnet_id = "${var.subnet_id}"
private_ip_address_allocation = "dynamic"
public_ip_address_id = "${azurerm_public_ip.public_ip.id}"
}
}
resource "azurerm_network_interface" "nic" {
count = "${1 - var.assign_public_ip}"
name = "${format("${var.name}-nic%02d", count.index)}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
ip_configuration {
name = "ip_cfg"
subnet_id = "${var.subnet_id}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_virtual_machine" "centos" {
count = "${var.count}"
name = "${format("${var.name}%02d", count.index)}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
network_interface_ids = ["${var.assign_public_ip == 1 ? azurerm_network_interface.nic_with_public_ip.id : azurerm_network_interface.nic.id }"]
vm_size = "${var.size}"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "OpenLogic"
offer = "CentOS"
sku = "7.3"
version = "latest"
}
storage_os_disk {
name = "${format("${var.name}-osdisk%02d", count.index)}"
caching = "ReadWrite"
create_option = "FromImage"
}
os_profile {
computer_name = "${format("${var.name}%02d", count.index)}"
admin_username = "${var.admin_user}"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys = {
path = "/home/${var.admin_user}/.ssh/authorized_keys"
key_data = "${var.ssh_key}"
}
}
tags {
environment = "${var.name}"
}
}
这将失败,错误如下: 错误运行计划:1个错误发生:
* module.jumphost.azurerm_virtual_machine.centos: 1 error(s) occurred:
* module.jumphost.azurerm_virtual_machine.centos: Resource 'azurerm_network_interface.nic' not found for variable 'azurerm_network_interface.nic.id'
A “图示表述” 可以用于获取从资源块创建的实例的属性值的列表,其中count
:
azurerm_network_interface.nic_with_public_ip.*.id
当count = 0
返回空列表。在这样的地方在任何时候的情况总共有一个在所有计数的,它可能利用此与concat
选择哪一个存在:
network_interface_ids = "${concat(azurerm_network_interface.nic_with_public_ip.*.id, azurerm_network_interface.nic.*.id)}"
由于network_interface_ids
已经是一个列表,我们可以只这里直接指定concat
的结果。由于count
正被分配在这些资源上,我们知道这个列表总是只有一个元素,从而达到了选择活动元素的期望结果。
在资源azurerm_virtual_machine
,变化到azurerm_network_interface.nic.*.id
请问你能更明确吗?我试着用两种方式回答你的问题,但都无效。将行更改为 'network_interface_ids = [“$ {var.assign_public_ip == 1?azurerm_network_interface.nic_with_public_ip。*。id:azurerm_network_interface.nic。*。id}”]'或'network_interface_ids = [“$ {var.assign_public_ip = = 1?azurerm_network_interface.nic_with_public_ip.id:azurerm_network_interface.nic。*。id}“]'都出错 – kenlukas
这很好,谢谢你的帮助。我对作业做了一个小修改,将其作为一个列表。再次感谢 – kenlukas