我正在撰寫 ansible playbook,我需要從 JSON API 結果中獲取一個值。我想在不知道前一個物件(sd#eureka ...)的情況下獲取鍵“1h”的值,并且我需要遍歷我的所有結果和所有桶。
{
"etat_ms_rep": {
"results": [
{
"buckets": {
"sd#eureka#pixidanalyticsbackend#c3fb422bb36882d9f502092fd75fcb34": {
"1h": -1,
"1m": -1
},
"sd#eureka#pixidanalyticsbackend#348fdab155904e22ca0c744d0c052cf8": {
"1h": 100,
"1m": 100
}
}
},
{
"buckets": {
"sd#eureka#pixidorchestratorbackend#8fa3441c6c5caa2d5f0e3264a00be91b": {
"1h": 100,
"1m": 100
},
"sd#eureka#pixidorchestratorbackend#6dc48be83cb86ae1a73b344e9421ed8e": {
"1h": 100,
"1m": 100
}
}
}
]
}
}
我試過了,但沒有成功,它沒有顯示“1h”鍵的值......
- name: Display all bucket info
set_fact:
test: "{{ etat_ms_rep.results | json_query(jmesquery) }}"
vars:
jmesquery: " [*].['1h'] "
uj5u.com熱心網友回復:
例如
- name: Get the value of the key 1h
debug:
msg: "{{ etat_ms_rep.results | json_query(jmesquery) }}"
vars:
jmesquery: '[].*.*.["1h"]'
給
msg:
- - - - -1
- - 100
- - - - 100
- - 100
如果您愿意,可以將串列展平
- name: Get the value of the key 1h
debug:
msg: "{{ etat_ms_rep.results | json_query(jmesquery) | flatten }}"
vars:
jmesquery: '[].*.*.["1h"]'
給
msg:
- -1
- 100
- 100
- 100
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/353745.html
