如何使用Jmespath过滤以相似字符串键开头的多个键名?
How to filter multiple key name start with similar string key using Jmespath?
我有下面的 json,我想从中过滤所有以键 macaddress_ 开头的所有 mac 地址,使用 jmespath 如何实现?
{
"facts_hash": {
"macaddress_em1": "44:a8:42:27:c7:ba",
"netmask_em1": "255.255.255.0",
"mtu_em1": "1500",
"macaddress_em2": "44:a8:42:27:c7:bb",
"mtu_em2": "1500",
"macaddress_em3": "44:a8:42:27:c7:bc",
"mtu_em3": "1500",
"macaddress_em4": "44:a8:42:27:c7:bd",
"mtu_em4": "1500",
}
}
谢谢
可以 select 匹配键并提取值,而不是 json_query。例如
- debug:
msg: "{{ my_keys|
map('extract', facts_hash)|
list }}"
vars:
my_keys: "{{ facts_hash.keys()|
select('match', '^macaddress_(.*)$')|
list }}"
给予
"msg": [
"44:a8:42:27:c7:ba",
"44:a8:42:27:c7:bb",
"44:a8:42:27:c7:bc",
"44:a8:42:27:c7:bd"
]
我有下面的 json,我想从中过滤所有以键 macaddress_ 开头的所有 mac 地址,使用 jmespath 如何实现?
{
"facts_hash": {
"macaddress_em1": "44:a8:42:27:c7:ba",
"netmask_em1": "255.255.255.0",
"mtu_em1": "1500",
"macaddress_em2": "44:a8:42:27:c7:bb",
"mtu_em2": "1500",
"macaddress_em3": "44:a8:42:27:c7:bc",
"mtu_em3": "1500",
"macaddress_em4": "44:a8:42:27:c7:bd",
"mtu_em4": "1500",
}
}
谢谢
可以 select 匹配键并提取值,而不是 json_query。例如
- debug:
msg: "{{ my_keys|
map('extract', facts_hash)|
list }}"
vars:
my_keys: "{{ facts_hash.keys()|
select('match', '^macaddress_(.*)$')|
list }}"
给予
"msg": [
"44:a8:42:27:c7:ba",
"44:a8:42:27:c7:bb",
"44:a8:42:27:c7:bc",
"44:a8:42:27:c7:bd"
]