使用json查询ansible从json中提取多个值
我有这样一个文件:使用json查询ansible从json中提取多个值,json,parsing,ansible,json-query,Json,Parsing,Ansible,Json Query,我有这样一个文件: { "account": "/system1/test", "description": "", "desired_state": "active", "id": "f957e20c-0033-4523-9020-e31304401149", "status": "active", "tags": [], "time_audited": "2018-01-
{
"account": "/system1/test",
"description": "",
"desired_state": "active",
"id": "f957e20c-0033-4523-9020-e31304401149",
"status": "active",
"tags": [],
"time_audited": "2018-01-14T23:12:22Z",
"time_created": "2018-01-14T22:34:53Z",
"time_updated": null
}
当我尝试获取一个值时,如下所示:
with_items: "{{orch_details.content|from_json|json_query('status')}}"
一切都很好
我想提取几个其他值,但无法将它们组合到json\u查询中,例如:
with_items: "{{orch_details.content|from_json|json_query('status, account, description')}}"
我的语法有什么问题?我想这条评论是想指出json_query
使用JMESPath作为过滤器。你可以在网站上读一读
在您的示例中,您可以执行以下操作来获得一个值数组:
with_items: "{{orch_details.content|from_json|json_query('[status, account, description]')}}"
这将返回:
[
"active",
"/system1/test",
""
]
{
"status": "active",
"account": "/system1/test",
"desc": ""
}
或者,如果您想要一个新的散列,可以:
with_items: "{{orch_details.content|from_json|json_query('{"status": status, "account": account, "desc": description]')}}"
这将返回:
[
"active",
"/system1/test",
""
]
{
"status": "active",
"account": "/system1/test",
"desc": ""
}
在发布SO之前检查一下怎么样?谢谢Andy,你的解决方案很好,我是个新手:(,文档确实显示了一些类似的内容。评论试图指出SO上的问题应该显示出来