gpt4 book ai didi

json - 在 Ansible 中过滤 JSON 文档

转载 作者:行者123 更新时间:2023-12-05 08:41:52 24 4
gpt4 key购买 nike

我有一个来自 GitHub 存储库的 JSON 回复,其中包含某个版本的可能下载列表(文档中的 assets 数组)。

当 Assets 的 namex64.AppImage 结尾时,我想获取浏览器下载 URL

在 Ansible 中,过滤器是根据 jmespath 构建的并使用其终端工具,我可以使用以下表达式查询 url:

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.jsonassets 数组中的第一个对象。

如何让 Ansible 遍历所有 assets 数组并应用我的过滤器?

附言:

如果不是查询 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"
}

最佳答案

JMESPath 过滤器中的类型错误问题在 issue 27299 中讨论。 .

您可以使用这个 patched json_query.py过滤器插件。

或者将双重转换应用于您的对象作为解决方法:| to_json | from_json |.
这会将对象转换为 JSON(因此是纯字符串)并返回,因此 json_query 会将字符串视为受支持的类型。

关于json - 在 Ansible 中过滤 JSON 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46738081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com