【问题标题】:terraform network_interface_ids - Inappropriate value for attributeterraform network_interface_ids - 属性值不合适
【发布时间】:2022-02-18 19:22:29
【问题描述】:

我正在尝试将“azurerm_virtual_machine”中的“network_interface_ids”设置为我的一个模块的输出,但在“terraform plan”期间收到以下错误并且无法弄清楚我哪里出错了:

│ Error: Incorrect attribute value type
│ 
│   on modules/virtualmachine/main.tf line 6, in resource "azurerm_virtual_machine" "vm":
│    6:   network_interface_ids             = [var.nicid]
│     ├────────────────
│     │ var.nicid is a list of string, known only after apply
│ 
│ Inappropriate value for attribute "network_interface_ids": element 0:
│ string required.
╵
##[error]Bash exited with code '1'.

我确信这很简单,我可以让它工作而没有被分成模块,只是无法弄清楚。我在下面附上了所有相关代码(显然删除了大部分代码以帮助简洁):

main.tf

  module "virtualmachine" {
  source                   = "./modules/virtualmachine"
  nicid                    = module.networking.nicidoutput
  }

模块/网络/main.tf

resource "azurerm_network_interface" "nic" {
  name                = var.nicname
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

modules/networking/outputs.tf

output "nicidoutput" {
    value = azurerm_network_interface.nic.id
}

模块/虚拟机/main.tf

  resource "azurerm_virtual_machine" "vm" {
  network_interface_ids             = [var.nicid]
  }

模块/虚拟机/variables.tf

variable "nicid" {
    type = list(string)
    description = "network interface id"
}

【问题讨论】:

  • networking 模块是否在任何地方调用?
  • 是的,我正在使用网络模块中的其他资源,没有任何错误
  • 您介意将该块按原样添加到问题中吗?
  • @MarkoE 感谢您的帮助!但是,下面接受的答案是解决方案。

标签: azure terraform terraform-provider-azure


【解决方案1】:

您的var.nicid 已经是一个列表。所以应该是:

network_interface_ids             = var.nicid

更新:

virtualmachine中的以下内容也应更改

nicid                    = [module.networking.nicidoutput] for virtualmachine

【讨论】:

  • 感谢您的回答。但是,当我进行更改时,出现以下错误:Error: Invalid value for module argument │ │ on main.tf line 75, in module "virtualmachine": │ 75: nicid = module.networking.nicidoutput │ │ The given value is not suitable for child module variable "nicid" defined │ at modules/virtualmachine/variables.tf:33,1-17: list of string required. ╵ ##[error]Bash exited with code '1'.
  • @vegedezozu 你可以试试nicid = [module.networking.nicidoutput] 换成virtualmachine
  • 非常感谢!有用!我知道这很简单。比较郁闷>_
  • 当然,您是否想在上面的评论中添加答案,以便将所有内容集中在一个地方?
  • @vegedezozu 我更新了答案。
猜你喜欢
  • 2021-12-14
  • 2021-11-03
  • 2021-06-23
  • 2021-11-27
  • 1970-01-01
  • 2020-04-09
  • 2020-08-09
  • 2021-07-17
  • 2021-12-09
相关资源
最近更新 更多