Xtz
Xtz

Reputation: 1

Get the values from ansible json stdout_lines

Given this output

 "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\"",
            "  },",
           "]"
        ]

I am trying to iterate through the loop and get all the values of the "name,entityFriendlyName and status " in a list i have the below task . but its not working

    - 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') }}"

Expected answer:

eg:

"name": "6ebba5b",", "entityFriendlyName": "SYSTEMDB [abc]"", "status": "Completed",",

Upvotes: 0

Views: 395

Answers (1)

Kristian
Kristian

Reputation: 3461

It seems you are trying to parse some JSON output from a command, something similiar to this:

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

If that's the case, out.stdout contains a string representation of the valid JSON structure and you should use from_json filter on that instead.

Secondly, the set_fact does not make sense here, so I will make a demo using debug instead:

- 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 }}"

This would generate:

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"
    }
}

Upvotes: 1

Related Questions