我有来自 GitHub 存储库的 JSON 回复,其中包含某个版本的可能下载列表(assets
文档中的数组)。
我想在资产以 . 结尾时获取浏览器下载 URL。name
x64.AppImage
在 Ansible 中,过滤器是构建在 apon 上的,jmespath
并且使用它的终端工具,我可以使用以下表达式查询 url:
assets[?ends_with(name, 'x64.AppImage')].browser_download_url
使用以下 playbook,查询 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"
}