[英]how to get json_query display property name in ansible playbook
我以這個 json 塊為例:
"msg": {
"10.10.28.10": {
"core": 23,
"cpuCoreUsage": 0.0,
"cputhreshold": 80,
"status": "healthy",
"status_code": 0,
"status_reason": "Checks passed",
"timestamp": 1614281443,
"total": 0
},
"10.10.28.5": {
"core": 18,
"cpuCoreUsage": 2.0,
"cputhreshold": 80,
"status": "healthy",
"status_code": 0,
"status_reason": "Checks passed",
"timestamp": 1614281443,
"total": 0
},
"capacity": 1080
}
我試圖弄清楚如何讓這個 output 的屬性名稱和狀態看起來像這樣。
DESIRED OUTPUT:
IP: 10.10.28.5, status: healthy, status_code: 0
IP: 10.10.28.10, status: healthy, status_code: 0
我可以打印除 IP 部分以外的所有內容:
- name: STATUS QUERY
debug:
msg: "code: {{ item }}"
loop: "{{ data.json | json_query(status_code_query) | list }}"
vars:
status_code_query: "*.{statuscode: status_code, status: status}"
我不會為此使用 JMESPath,原因是,雖然它非常擅長查詢 JSON,但它並不擅長顯示 JSON 鍵。
keys()
function 是您可以在那里找到的最接近的,但它會為您生成一個數組,並且由於您無法返回父節點,因此您不能執行以下操作:
*.{IP: keys($)[0], statuscode: status_code, status: status}
雖然這是一個經常被請求的功能: https://github.com/jmespath/jmespath.js/issues/22
現在要解決您的用例,您可以使用keys()
function,但 Python 之一。
您還有一個問題,即data.json
的所有值都不是字典:在"capacity": 1080
中,值是一個簡單的int
。
您可以 go 使用when
測試和驗證您的值是否確實是一個mapping
(或者換句話說是一個字典)來解決這個奇怪的數據結構。
鑒於劇本:
- hosts: all
gather_facts: yes
tasks:
- debug:
msg: >-
IP: {{ item }},
status: {{ data.json[item].status }},
status_code: {{ data.json[item].status_code }}
loop: "{{ data.json.keys() }}"
when: data.json[item] is mapping
vars:
data:
json:
10.10.28.10:
core: 23
cpuCoreUsage: 0
cputhreshold: 80
status: healthy
status_code: 0
status_reason: Checks passed
timestamp: 1614281443
total: 0
10.10.28.5:
core: 18
cpuCoreUsage: 2
cputhreshold: 80
status: healthy-
status_code: 0-
status_reason: Checks passed
timestamp: 1614281443
total: 0
capacity: 1080
這產生了回顧:
PLAY [all] **********************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]
TASK [debug] ********************************************************************************************************
ok: [localhost] => (item=10.10.28.10) => {
"msg": "IP: 10.10.28.10, status: healthy, status_code: 0"
}
ok: [localhost] => (item=10.10.28.5) => {
"msg": "IP: 10.10.28.5, status: healthy-, status_code: 0-"
}
skipping: [localhost] => (item=capacity)
PLAY RECAP **********************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
也就是說,它也是dict2items
過濾器的完美用例:
- hosts: all
gather_facts: yes
tasks:
- debug:
msg: >-
IP: {{ item.key }},
status: {{ item.value.status }},
status_code: {{ item.value.status_code }}
loop: "{{ data.json | dict2items }}"
when: item.value is mapping
vars:
data:
json:
10.10.28.10:
core: 23
cpuCoreUsage: 0
cputhreshold: 80
status: healthy
status_code: 0
status_reason: Checks passed
timestamp: 1614281443
total: 0
10.10.28.5:
core: 18
cpuCoreUsage: 2
cputhreshold: 80
status: healthy-
status_code: 0-
status_reason: Checks passed
timestamp: 1614281443
total: 0
capacity: 1080
將產生相同的回顧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.