带有 CSV 文件的天蓝色 Terraform 参数 [英] azure Terraform parameter with CSV file
问题描述
我正在尝试使用 CSV 文件访问 terraform 变量数据,创建资源组并将资源组的名称添加到 CSV 文件中并尝试访问代码.
代码如下:
本地人{Resource_groupname = csvdecode(file("${path.module}/onkar.csv"))}//创建资源组资源azurerm_resource_group"Customer11"{计数 = 长度(local.Resource_groupname)name = local.Resource_groupname[count.index].resourcegroup_name位置=北欧"}
我收到以下错误:
在 admin.tf 第 15 行,在资源azurerm_resource_group"中客户 11":15:姓名 =local.Resource_groupname[count.index].resourcegroup_name|----------------|local.Resource_groupname 是具有 1 个元素的对象列表此对象没有名为resourcegroup_name"的属性.
更新
您可以尝试输出 local.Resource_groupname
以查看从 CSV 文件加载数据时的样子.
更新2
我真的不明白为什么你不知道该怎么做.这是我对所有东西的测试截图,希望你解决它:
Terraform 文件和 CSV 的内容.
Terraform 计划:
I'm trying to access terraform variable data using CSV file, have creating resource group and the name of resource group are added into the CSV file and trying to access into code.
Here is the code :
locals {
Resource_groupname = csvdecode(file("${path.module}/onkar.csv"))
}
//Create a resource group
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].resourcegroup_name
location = "North europe"
}
I get the following error :
on admin.tf line 15, in resource "azurerm_resource_group"
"Customer11": 15: name =
local.Resource_groupname[count.index].resourcegroup_name
|----------------
| local.Resource_groupname is list of object with 1 element
This object does not have an attribute named "resourcegroup_name".
Updated
Updated Code:
locals {
Resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].group_names
location = "North europe"
}
New Updated
locals {
Resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].Resource_groupname
location = "North europe"
}
New update CSV file and output
To load the input from the CSV file, I assume your CSV file with only one line and it looks like this:
test1,test2,test3
Then you can load and use them from the CSV file as below:
locals {
group_names = split(",", file("./test.csv"))
}
resource "azurerm_resource_group" "Customer11" {
count = length(local.group_names)
name = local.group_names[count.index]
location = "North europe"
}
If you use the CSV file like this:
resource_group_name
test1
test2
test3
Then the terraform code should change into this:
locals {
group_names = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "main" {
count = length(local.group_names)
name = local.group_names[count.index].resource_group_name
location = "East US"
}
Update:
With the CSV file you provide, you need to change the resource group name like this:
resource "azurerm_resource_group" "Customer11" {
count = length(local.Resource_groupname)
name = local.Resource_groupname[count.index].group_names # here is the change
location = "North europe"
}
Here is the screenshot of the CSV file:
You can try to output the local.Resource_groupname
to see what it looks like when you load the data from the CSV file.
Update2
I really do not understand why you cannot figure out how to do it. Here is the screenshot of my test for all the things, hope you solve it:
The content of the Terraform file and the CSV.
The Terraform plan:
这篇关于带有 CSV 文件的天蓝色 Terraform 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!