从ansible json stdout_lines获取值

sdnqo3pr  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(175)

给定此输出

"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]",“状态”:“已完成”、",

dm7nw8vv

dm7nw8vv1#

看起来您正在尝试解析某个命令的JSON输出,类似于以下内容:

- command: command-that-produces-json
  register: out

如果是这种情况,out.stdout包含有效JSON结构的字符串表示,您应该在其上使用from_json filter。
其次,set_fact在这里没有意义,所以我将使用debug进行演示:

- name: print all entities
  # You can replace the debug task with anything you wish
  ansible.builtin.debug:
    msg:
      name: "{{ item.name }}"
      entity_friendly_name: "{{ item.properties.entityFriendlyName }}"
      status: "{{ item.properties.status }}"
  # Loop the output and set custom label to unclutter the output
  loop: "{{ out.stdout | from_json }}"
  loop_control:
    label: "{{ item.name }}"

这将产生:

ok: [localhost] => (item=6ebba5b) => {
    "msg": {
        "entity_friendly_name": "SYSTEMDB [abc]",
        "name": "6ebba5b",
        "status": "Completed"
    }
}
ok: [localhost] => (item=3e6738d5) => {
    "msg": {
        "entity_friendly_name": "sid [abc]",
        "name": "3e6738d5",
        "status": "Completed"
    }
}

相关问题