Return 顶级密钥基于子对象中密钥的存在
Return top level key based on presence of key at subobject
我有一个这样的 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:
我在查找虚拟机时尝试获取组,例如我查找 group_1 我想获取
apps_prometheus
apps_alertmanager
我正在尝试将 kislyuk/yq 和 jq 与映射/键/包含
结合使用
----- 用路径命令测试
如果我使用命令
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"
]
我如何使用命令路径来获取空值?
在 kislyuk/yq 中,您可以使用 jq
中可用的 paths
函数来过滤键名
yq -r '.apps_membership.children | paths | select(.[-1] == "group_1")[0]'
我有一个这样的 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:
我在查找虚拟机时尝试获取组,例如我查找 group_1 我想获取
apps_prometheus
apps_alertmanager
我正在尝试将 kislyuk/yq 和 jq 与映射/键/包含
结合使用----- 用路径命令测试
如果我使用命令
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"
]
我如何使用命令路径来获取空值?
在 kislyuk/yq 中,您可以使用 jq
中可用的 paths
函数来过滤键名
yq -r '.apps_membership.children | paths | select(.[-1] == "group_1")[0]'