【问题标题】:Experiencing an error when try to output content of a csv file using terraform尝试使用 terraform 输出 csv 文件的内容时遇到错误
【发布时间】:2021-09-03 21:16:21
【问题描述】:

我正在尝试使用 terraform 变量数据(CSV 文件)来创建资源组,并将资源组的名称添加到 CSV 文件中。我目前遇到以下错误

│错误:不支持的属性 │ │ 在 testtf.tf 第 11 行,资源“azurerm_resource_group”“Main”中: │ 11: name = local.resource_groupname[count.index].groupname │ ├──────────────── │ │ count.index 是一个数字,只有 apply 后才知道 │ │ local.resource_groupname 是具有 3 个元素的对象列表 │ │ 此对象没有名为“groupname”的属性。

代码

provider "azurerm" {
    features{}
}

locals {
      resource_groupname = csvdecode(file("./test.csv"))
    }

    resource "azurerm_resource_group" "Main" {
      count    = length(local.resource_groupname)
      name     =  local.resource_groupname[count.index].groupname   
      location = "North europe"
    }

./test.csv 内容

https://drive.google.com/file/d/1ituKDzaMVXnyynkjLBZRzMdWK9tnkL14/view?usp=sharing

【问题讨论】:

  • 我看不出它为什么不起作用的任何原因。也许,就像以前一样,您发布了错误的代码、错误的错误消息或错误的 csv 文件。请仔细检查。
  • 这次信息是正确的。我可以遇到类似stackoverflow.com/questions/60986320/… 之类的问题,但我的解决方案不是很清楚。

标签: terraform terraform-provider-azure terraform0.12+ terraform-template-file terraform-provider-openstack


【解决方案1】:

我认为您提供的文件包含导致 TF 和/或 Azure 阻塞的 UTF BOM(字节顺序标记)字节。

我将 csv 文件重新创建为纯 ascii,您的 HCL 工作正常

我通过使用terraform console 发现了额外的字符。这是排查 TF 错误的一种非常简单快捷的方法。

我使用这个非常基本的 .tf 文件来检查 cvsdecode() 的行为。 (下面的 test0.csv 是您的原始文件,而 test.csv 是我从头开始创建的文本文件):

locals {
      resource_groupname0 = csvdecode(file("./test0.csv"))
      resource_groupname = csvdecode(file("./test.csv"))
 }

运行 terraform 控制台并检查局部变量。注意“组名”(test0.csv)之前的 BOM 字符:

$ terraform console
> local.resource_groupname0
tolist([
  {
    "\ufeffgroupname" = "test11"
  },
  {
    "\ufeffgroupname" = "test12"
  },
  {
    "\ufeffgroupname" = "test13"
  },
])
> local.resource_groupname
tolist([
  {
    "groupname" = "test11"
  },
  {
    "groupname" = "test12"
  },
  {
    "groupname" = "test13"
  },
])

同样使用unix文件命令:

## Your file
$ file test0.csv
test0.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators

## Created by hand with text editor
$ file test.csv
test.csv: ASCII text

【讨论】:

  • 这实际上有帮助.....我刚才所做的是将文件保存为逗号分隔的格式,它工作正常。
  • 很高兴。请将答案标记为有用,以帮助可能遇到相同问题的其他人
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多