在ansible中提取和连接json节点

时间:2016-09-30 12:02:18

标签: ansible jinja2 ansible-playbook

我有Ansible任务,其中输出已注册 它是由curl/uri模块调用的弹性支持请求的响应。

一个例子: { "changed": false, "msg": "All items completed", "results": [ { "_ansible_item_result": true, "_ansible_no_log": false, "changed": false, "content_length": "747", "content_type": "application/json; charset=UTF-8", "invocation": {...}, "item": "content", "json": { "snapshots": [ { "duration_in_millis": 2863, "end_time": "2016-09-25T16:00:05.357Z", "end_time_in_millis": 1474819205357, "failures": [], "indices": [ "content_h_20160817_1155", "content_s_20160817_1159", "content_p_20160817_1158", "content_t_20160909_0451", "content_v_20160909_0454", "content_h_20160909_0431", "content_v_20160817_1201", "content_m_20160817_1157", "content_m_20160909_0438", "content_p_20160909_0443", "content_t_20160817_1200", "content_i_20160817_1156", "content_i_20160909_0434", "content_s_20160909_0447" ], "shards": { "failed": 0, "successful": 28, "total": 28 }, "snapshot": "snapshot_content_20160925_1600", "start_time": "2016-09-25T16:00:02.494Z", "start_time_in_millis": 1474819202494, "state": "SUCCESS", "version": "2.3.3", "version_id": 2030399 } ] }, "msg": "OK (747 bytes)", "redirected": false, "status": 200, "url": "url" }, { "_ansible_item_result": true, "_ansible_no_log": false, "changed": false, "content_length": "526", "content_type": "application/json; charset=UTF-8", "invocation": {...}, "item": "log", "json": { "snapshots": [ { "duration_in_millis": 5002, "end_time": "2016-09-25T16:01:24.174Z", "end_time_in_millis": 1474819284174, "failures": [], "indices": [ "log_i_20160804_0749", "log_p_20160804_0811", "log_v_20160804_0847", "log_m_20160804_0758", "log_h_20160804_0746", "log_t_20160804_0829", "log_s_20160804_0825" ], "shards": { "failed": 0, "successful": 14, "total": 14 }, "snapshot": "snapshot_log_20160925_1600", "start_time": "2016-09-25T16:01:19.172Z", "start_time_in_millis": 1474819279172, "state": "SUCCESS", "version": "2.3.3", "version_id": 2030399 } ] }, "msg": "OK (526 bytes)", "redirected": false, "status": 200, "url": "url" } ] } 接下来,我需要从结果中的每个元素中仅获得indices [] 如何从所有indices[]个节点中将result[]数组提取到一个变量中,以便以后能够使用with_items? 感谢。

PS。我省略了invocation元素,它们太长了。

1 个答案:

答案 0 :(得分:1)

您可能希望了解mapsum 回答你的问题:

curl_res.results | map(attribute='json.snapshots') | sum(start=[]) | map(attribute='indices') | sum(start=[])

您需要使用sum两次展开列表,因为snapshots也是一个列表。