2017-08-18 232 views
2

我在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' 

回答

2

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正被分配在這些資源上,我們知道這個列表總是隻有一個元素,從而達到了選擇活動元素的期望結果。

+0

這很好,謝謝你的幫助。我對作業做了一個小修改,將其作爲一個列表。再次感謝 – kenlukas

0

在資源azurerm_virtual_machine,變化到azurerm_network_interface.nic.*.id

+0

請問你能更明確嗎?我試着用兩種方式回答你的問題,但都無效。將行更改爲 '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