首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >terraform从列表中获取索引值

terraform从列表中获取索引值
EN

Stack Overflow用户
提问于 2021-07-31 09:46:08
回答 1查看 80关注 0票数 0

如何将索引值赋给terraform变量

让我们说:

我的示例输入看起来是terraform.tfvars.json:

代码语言:javascript
复制
{
    "resource_groups": [
        {
            "name_suffix": "AI",
            "location": "westus2",
            "is_default": false
        },
        {
            "name_suffix": "Montoring",
            "location": "westus2",
            "is_default": false
        },
        {
            "name_suffix": "Base",
            "location": "westus2",
            "is_default": false
        },
        {
            "name_suffix": "Core",
            "location": "westus2",
            "is_default": true
        }

    ]
}

main.tf

代码语言:javascript
复制
 locals {
 # I tried like 

 default_rg_index = [for rg, index in var.resource_groups: index if try(rg.is_default, false) == true]
}

我期望default_rg_index分配3,但它不起作用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-31 09:57:52

rg, index应该是相反的。你也可以让它变得更简单:

代码语言:javascript
复制
default_rg_index = [for index, rg in var.resource_groups: index if rg.is_default]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68600954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档