gpt4 book ai didi

azure - Terraform:使用 If/Else 模式将资源属性分配给资源参数

转载 作者:行者123 更新时间:2023-12-02 07:48:51 25 4
gpt4 key购买 nike

我正在使用IF/ELSE pattern在 Terraform 中构建具有或不具有公共(public) IP 的 NIC。分配 NIC 时会出现问题。我找不到一种技术可以让我选择用于创建 NIC 的资源(有或没有公共(public) IP)。使用三元运算失败,因为其中一项资源不存在。将资源放入列表中不会进行插值。如何分配正确的资源输出?

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'

最佳答案

“splat 表达式”可用于获取从具有 count 的资源 block 创建的实例的属性值列表:

azurerm_network_interface.nic_with_public_ip.*.id

count = 0 时,这会返回一个空列表。在这种情况下,所有计数中始终只有 1,可以利用 concat 来选择存在的那个:

network_interface_ids = "${concat(azurerm_network_interface.nic_with_public_ip.*.id, azurerm_network_interface.nic.*.id)}"

由于network_interface_ids已经是一个列表,我们可以直接在这里分配concat的结果。由于在这些资源上分配 count 的方式,我们知道该列表始终只有一个元素,从而达到选择事件元素的预期结果。

关于azure - Terraform:使用 If/Else 模式将资源属性分配给资源参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45760269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com