You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Terraform HCL表达式筛选含ns的列表元素

Terraform 列表过滤实现方案

首先需要修正原列表的语法,Terraform中字符串元素必须用双引号包裹,正确的列表定义应为:

namespaces = ["blue", "red", "green", "ns-blue", "ns-green", "ns-grey"]

要筛选出包含"ns"的元素,可使用Terraform的for表达式结合contains函数实现,具体表达式如下:

namespace-filtered = [for ns in namespaces : ns if contains(ns, "ns")]

该表达式会遍历namespaces列表中的每个元素ns,仅保留满足contains(ns, "ns")(即元素包含子串"ns")的项,最终得到预期结果:

namespace-filtered = ["ns-blue", "ns-green", "ns-grey"]

内容的提问来源于stack exchange,提问作者yelmir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 09:25:41