我有一個來自GitHub存儲庫的JSON回覆,其中包含某個版本的可能下載列表(文檔中的assets
數組)。在Ansible中過濾JSON文檔
我想獲取瀏覽器下載URL當資產的name
以x64.AppImage
結尾時。
在Ansible,過濾器是內置APON jmespath
和使用它的終端工具,我可以用下面的表達式查詢的網址:
assets[?ends_with(name, 'x64.AppImage')].browser_download_url
用下面的劇本中,JSON文檔被查詢,並存儲在json_reply
變量。
---
- hosts: local
tasks:
- name: Get list of Rambox releases
uri:
url: "https://api.github.com/repos/saenzramiro/rambox/releases/latest"
body_format: json
register: json_reply
- name: Filter reply
debug: URL -> "{{ item }}"
with_items:
- "{{ json_reply.json | json_query(json_filter) }}"
vars:
- json_filter: assets[?ends_with(name, 'x64.AppImage')].browser_download_url
但是,在執行此提供了以下錯誤:
fatal: [localhost]: FAILED! => {
"msg": "JMESPathError in json_query filter plugin:\nIn function ends_with(), invalid type for value: latest-mac.json, expected one of: ['string'], received: \"unknown\""
}
latest-mac.json
哪裏是assets
陣列中的第一個對象。
如何讓Ansible遍歷所有assets
數組並應用我的過濾器?
PS:
如果不是查詢,如果name
用一句話結束我直接指定,過濾器的工作原理:
assets[?name == 'Rambox-0.5.13-x64.AppImage')].browser_download_url
JSON例如:
{
"url": "https://api.github.com/repos/saenzramiro/rambox/releases/8001922",
"prerelease": false,
"created_at": "2017-10-04T21:14:15Z",
"published_at": "2017-10-05T01:10:55Z",
"assets": [
{
"url": "https://api.github.com/repos/saenzramiro/rambox/releases/assets/4985942",
"id": 4985942,
"name": "latest-mac.json",
"uploader": {
"login": "saenzramiro",
"id": 2694669
},
"browser_download_url": "https://github.com/saenzramiro/rambox/releases/download/0.5.13/latest-mac.json"
},
{
"url": "https://api.github.com/repos/saenzramiro/rambox/releases/assets/4985640",
"id": 4985640,
"name": "Rambox-0.5.13-x64.AppImage",
"uploader": {
"login": "saenzramiro",
"id": 2694669
},
"browser_download_url": "https://github.com/saenzramiro/rambox/releases/download/0.5.13/Rambox-0.5.13-x64.AppImage"
}
],
"tarball_url": "https://api.github.com/repos/saenzramiro/rambox/tarball/0.5.13"
}
的問題是在這裏討論:https://github.com/ansible/ansible/issues/27299#issuecomment-331068246和解決方法就是使用'| to_json | from_json |' –
@KonstantinSuvorov你提出的解決辦法是最好的解決方案,因爲它避免了循環在上述文檔。如果您發佈答案,我會接受它。 –