给定此输出
"stdout_lines": [
"[",
" {",
" \"eTag\": null,",
" \"id\": \"abc\",",
" \"location\": null,",
" \"name\": \"6ebba5b\",",
" \"properties\": {",
" \"actionsInfo\": null,",
" \"activityId\": \"f014bc98\",",
" \"backupManagementType\": \"load\",",
" \"duration\": \"0:02:59.023742\",",
" \"endTime\": \"2022-11-07T19:33:00.522831+00:00\",",
" \"entityFriendlyName\": \"SYSTEMDB [abc]\",",
" \"errorDetails\": null,",
" \"extendedInfo\": null,",
" \"isUserTriggered\": false,",
" \"jobType\": \"loadJob\",",
" \"operation\": \"Backup (Full)\",",
" \"startTime\": \"2022-11-07T19:30:01.499089+00:00\",",
" \"status\": \"Completed\",",
" \"workloadType\": \"base\"",
" },",
" \"resourceGroup\": \"abc\",",
" \"tags\": null,",
" \"type\": \"backupJobs\"",
" },",
" {",
" \"eTag\": null,",
" \"id\": \"efg\",",
" \"location\": null,",
" \"name\": \"3e6738d5\",",
" \"properties\": {",
" \"actionsInfo\": null,",
" \"activityId\": \"8265cb659\",",
" \"backupManagementType\": \"load\",",
" \"duration\": \"0:12:59.319233\",",
" \"endTime\": \"2022-11-07T19:43:00.788547+00:00\",",
" \"entityFriendlyName\": \"sid [abc]\",",
" \"errorDetails\": null,",
" \"extendedInfo\": null,",
" \"isUserTriggered\": false,",
" \"jobType\": \"loadJob\",",
" \"operation\": \"Backup (Full)\",",
" \"startTime\": \"2022-11-07T19:30:01.469314+00:00\",",
" \"status\": \"Completed\",",
" \"workloadType\": \"base\"",
" },",
" \"resourceGroup\": \"abc\",",
" \"tags\": null,",
" \"type\": \"backupJobs\"",
" },",
"]"
]
我尝试迭代循环并获取列表中“name,entityFriendlyName and status“的所有值。我有以下任务。但它不起作用
- set_fact:
name: "{{ out |from_json|json_query('.stdout_lines[].name') }}"
entity_friendly_name: "{{ out |from_json|json_query('.stdout_lines[].properties.entityFriendlyName') }}"
status: "{{ out |from_json|json_query('.stdout_lines[].properties.status') }}"
预期答案:
例如:
“名称”:“6ebba5b”,",“实体友好名称”:“系统数据库[abc]",“状态”:“已完成”、",
1条答案
按热度按时间dm7nw8vv1#
看起来您正在尝试解析某个命令的JSON输出,类似于以下内容:
如果是这种情况,
out.stdout
包含有效JSON结构的字符串表示,您应该在其上使用from_json
filter。其次,
set_fact
在这里没有意义,所以我将使用debug
进行演示:这将产生: