【发布时间】:2022-07-28 07:40:57
【问题描述】:
所以我在下面有一个 json 文件,我绝对可以根据需要对其进行重组,但它是我开始的。它必须是任何类型的 terraform 的外部格式。我基于看似简单的兼容性选择了json。
我希望能够遍历下面 json 文件中的所有正则表达式模式(我截断了这个列表),如果正则表达式模式与 testcomputer 匹配,则将 ou 值返回给另一个局部变量。一个警告,我不确定我是否有问题,是 json 需要转义 \\ 。在命令式编程语言中,我只是用一个 switch/case 节或执行评估的方法来处理这个问题。不知道如何最好地使用 HCL 做到这一点。
ou.json:
{
\"ProdInf\": {
\"regex\": \"/^(?i)nyinf-p(\\\\d+)/\",
\"ou\": \"OU=Inf,OU=Prod,OU=Servers,DC=example,DC=local\"
},
\"ProdHyperV\": {
\"regex\": \"/^(?i)nyhyp-p(\\\\d+)/\",
\"ou\": \"OU=HyperV,OU=Prod,OU=Servers,DC=example,DC=local\"
},
\"ProdRDS\": {
\"regex\": \"/^(?i)nyrds-p(\\\\d+)/\",
\"ou\": \"OU=RDS,OU=Prod,OU=Servers,DC=example,DC=local\"
}
}
我有一些初始的 terraform 代码(人为并基于我的实际问题),但我无法弄清楚如何从这里开始。:
locals {
testcomputer = \"nyhyp-p29\"
map1 = jsondecode(file(\"./ou.json\"))
# started to play with loops here, but not really sure how to do what I am want.
oulist = [for ou in local.map1 : ou].*.ou
regexlist = [for regex in local.map1 : regex].*.regex
test_computer_ou = # this is the desired value I am trying to get
}
标签: terraform terraform-provider-aws hcl