Search code examples
ansibleansible-2.xansible-inventoryansible-factsansible-awx

Creating a list from json output


Given the below output of application_stop_time_output, I want to create a list of dicts which contain only item and stdout. stdout should be an interger.

- debug:
     msg: "{{ application_stop_time_output }}"

gives:

ok: [ebppdoxs10] => {
    "msg": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "cmd": "application_stop_time_format=$(echo \"[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                "delta": "0:00:00.009048", 
                "end": "2023-01-20 23:26:19.995453", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "application_stop_time_format=$(echo \"[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "item": "[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
                "rc": 0, 
                "start": "2023-01-20 23:26:19.986405", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "1674142504", 
                "stdout_lines": [
                    "1674142504"
                ]
            }, 
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "cmd": "application_stop_time_format=$(echo \"[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                "delta": "0:00:00.009918", 
                "end": "2023-01-20 23:26:23.887901", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "application_stop_time_format=$(echo \"[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "item": "[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
                "rc": 0, 
                "start": "2023-01-20 23:26:23.877983", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "1674138844", 
                "stdout_lines": [
                    "1674138844"
                ]
            }
        ]
    }
}

Expected output:

ok: [ebppdoxs10] => {
    "msg": {
          "results": [
            {
               "item": "[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
               "stdout": 1674142504, 
            }, 
            {
              "item": "[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
              "stdout": 1674138844, 
           }
        ]
    }


Solution

  • You can use loop for this and iterate to the results list together with loop_control and label loo_control-label to avoid each long item (iteration) be displayed.

    Note that I have modified a bit the output but you can get the idea.

    - hosts: localhost
      vars:
        results: [
                  {
                      "ansible_loop_var": "item", 
                      "changed": true, 
                      "cmd": "application_stop_time_format=$(echo \"[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                      "delta": "0:00:00.009048", 
                      "end": "2023-01-20 23:26:19.995453", 
                      "failed": false, 
                      "invocation": {
                          "module_args": {
                              "_raw_params": "application_stop_time_format=$(echo \"[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                              "_uses_shell": true, 
                              "argv": null, 
                              "chdir": null, 
                              "creates": null, 
                              "executable": null, 
                              "removes": null, 
                              "stdin": null, 
                              "stdin_add_newline": true, 
                              "strip_empty_ends": true, 
                              "warn": true
                          }
                      }, 
                      "item": "[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
                      "rc": 0, 
                      "start": "2023-01-20 23:26:19.986405", 
                      "stderr": "", 
                      "stderr_lines": [], 
                      "stdout": "1674142504", 
                      "stdout_lines": [
                          "1674142504"
                      ]
                  }, 
                  {
                      "ansible_loop_var": "item", 
                      "changed": true, 
                      "cmd": "application_stop_time_format=$(echo \"[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                      "delta": "0:00:00.009918", 
                      "end": "2023-01-20 23:26:23.887901", 
                      "failed": false, 
                      "invocation": {
                          "module_args": {
                              "_raw_params": "application_stop_time_format=$(echo \"[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                              "_uses_shell": true, 
                              "argv": null, 
                              "chdir": null, 
                              "creates": null, 
                              "executable": null, 
                              "removes": null, 
                              "stdin": null, 
                              "stdin_add_newline": true, 
                              "strip_empty_ends": true, 
                              "warn": true
                          }
                      }, 
                      "item": "[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
                      "rc": 0, 
                      "start": "2023-01-20 23:26:23.877983", 
                      "stderr": "", 
                      "stderr_lines": [], 
                      "stdout": "1674138844", 
                      "stdout_lines": [
                          "1674138844"
                      ]
                  }
              ]
    
    
      tasks:
        - debug:
            msg: 
              - "{{ item.item }}"
              - "{{ item.stdout }}"
          loop: "{{ results }}"
          loop_control:
            label: item.item ## Here we use the item key as a label to limit the displayed output.
    

    Gives:

    TASK [debug] 
    ok: [localhost] => (item=item.item) => {
        "msg": [
            "[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war",
            "1674142504"
        ]
    }
    ok: [localhost] => (item=item.item) => {
        "msg": [
            "[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war",
            "1674138844"
        ]
    }