根据子对象中关键点的存在返回顶级关键点



我有一个类似的YAML文件

---
apps_membership:
children:
apps_prometheus:
children:                                                                                                       
group_1:
group_2:
apps_logstash:
children:
group_3:
group_4:
apps_alertmanager:
children:
group_1:
group_2:

当我寻找vm时,我会尝试获得组,例如,我寻找group_1我想获得

apps_prometheus
apps_alertmanager

我正在尝试将kislyuk/yq和jq与map/key/contents 相结合

-----使用路径命令进行测试

如果我使用命令yq -r '.apps_membership.children | paths'我得到这个结果:

[
"apps_prometheus"
]
[
"apps_prometheus",
"children"
]
[
"apps_logstash"
]
[
"apps_logstash",
"children"
]
[
"apps_alertmanager"
]
[
"apps_alertmanager",
"children"
]

如果我用换yaml

apps_alertmanager:
children:
group_1: ""
group_2: ""

我得到想要的输出:

[
"apps_alertmanager"
]
[
"apps_alertmanager",
"children"
]
[
"apps_alertmanager",
"children",
"group_1"
]
[
"apps_alertmanager",
"children",
"group_2"
]

如何使用命令路径也获取null值?

在kislyuk/yq中,您可以使用jq中可用的paths函数来筛选密钥名称

yq -r '.apps_membership.children | paths | select(.[-1] == "group_1")[0]'

相关内容

  • 没有找到相关文章

最新更新