# Parse the status code and update response

**URL:** <https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [October 26, 2023, 4:10pm UTC](https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979 "2023-10-26T16:10:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manjunath7](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@Manjunath7](https://forum.ansible.com/u/Manjunath7)\
**Post date:** [October 26, 2023, 4:10pm UTC](https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979/1 "2023-10-26T16:10:31Z")

</div>

this is the code I created and the response we are getting is something like this

```auto
---

- name: Get media_server response

hosts: localhost

gather_facts: no

tasks:

- name: Pushing the generated configuration file to Media Server (sfconnect)

shell: "curl -X PUT -H 'Username: AWSSUsername1' -H 'Password: AWSSPassword1' '[https://URL/awss-services/rest/v1/media/rgnet/rxg_gateways?'keepFileName=true](https://URL/awss-services/rest/v1/media/rgnet/rxg_gateways?%27keepFileName=true) -F 'file=@test_config_1.txt'"

args:

warn: no

register: mediasvr_resp

ignore_errors: true

- name: debug the upgrade_response

debug:

var: mediasvr_resp

- name: print the response code

debug:

msg: "{{ mediasvr_resp.stdout }}"

when: mediasvr_resp.stdout is defined

```

the response is :

```auto
ok: [localhost] => {

    "msg": {

        "changed": true,

        "cmd": "curl -X PUT -H 'Username: AWSSUsername1' -H 'Password: AWSSPassword1' 'https://URL/awss-services/rest/v1/media/rgnet/rxg_gateways?keepFileName=true' -F 'file=@/tmp/test_config_1.txt'",

        "delta": "0:00:00.076243",

        "end": "2023-10-13 02:43:30.159288",

        "failed": false,

        "msg": "",

        "rc": 0,

        "start": "2023-10-13 02:43:30.083045",

        "stderr": " % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\r100 362 100 162 100 200 2571 3174 --:--:-- --:--:-- --:--:-- 5746",

        "stderr_lines": [

            " % Total % Received % Xferd Average Speed Time Time Time Current",

            " Dload Upload Total Spent Left Speed",

            "",

            " 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0",

            "100 362 100 162 100 200 2571 3174 --:--:-- --:--:-- --:--:-- 5746"

        ],

        "stdout": "{\"fileName\": \"test_config_1.txt\", \"path\": \"/rgnet/rxg_gateways\", \"bytes\": \"5\", \"mimeType\": \"application/octet-stream\", \"modified\": \"Fri Oct 13 02:43:30 UTC 2023\"}",

        "stdout_lines": [

            "{\"fileName\": \"test_config_1.txt\", \"path\": \"/rgnet/rxg_gateways\", \"bytes\": \"5\", \"mimeType\": \"application/octet-stream\", \"modified\": \"Fri Oct 13 02:43:30 UTC 2023\"}"

        ]

    }

}

```

Now I have to write a task based on the fields present in the stdout in the response we have to parse stdout whether in stdout are present then it should print as success and if fields are missing then it should print the response as failure.

---

<div class="post-metadata">

**Author:** ![Leo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/leo/32/23_2.png) [@Leo](https://forum.ansible.com/u/Leo)\
**Post date:** [October 26, 2023, 4:12pm UTC](https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979/2 "2023-10-26T16:12:42Z")

</div>

Hello @Manjunath7 , I just moved your post from the #Guides, FAQs & Howtos category to the #Get Help category and fixed the formatting a bit.

You might want to clarify a bit on what you are trying to accomplish and what you need help with, it looks like you want to do the following:

> Now I have to write a task based on the fields present in the stdout in the response we have to parse stdout whether in stdout are present then it should print as success and if fields are missing then it should print the response as failure.

Do you need help with that? is something not working as expected or you are asking how to do it?

---

<div class="post-metadata">

**Author:** ![Manjunath7](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@Manjunath7](https://forum.ansible.com/u/Manjunath7)\
**Post date:** [October 26, 2023, 5:29pm UTC](https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979/3 "2023-10-26T17:29:13Z")

</div>

yes leo I’m expecting some response and not getting that so I’m asking how to achieve it?

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [October 26, 2023, 6:26pm UTC](https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979/4 "2023-10-26T18:26:43Z")

</div>

First off, I don’t know which Ansible version you’re using; I’m on Ansible core 2.15.4 and I get this error on your shell module task:

```bash
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (ansible.legacy.command) module: args. Supported parameters include: _raw_params, _uses_shell, argv, chdir, creates, executable, removes, stdin, stdin_add_newline, strip_empty_ends."}

```

Anyways, without setting `args` parameter, It works on my end:

```bash
TASK [print the response code] *********************************************************************************************************************************************************************************************************************************************************************************************
Thursday 26 October 2023 20:10:58 +0200 (0:00:00.015) 0:00:00.309 ******
ok: [localhost] => {
    "msg": "HTTP/2 301 \r\ndate: Thu, 26 Oct 2023 18:10:58 GMT\r\nlocation: https://www.ansible.com/\r\ncache-control: max-age=3600\r\nexpires: Thu, 26 Oct 2023 19:10:58 GMT\r\nreport-to: {\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=DeHsA8sgsoaKymu6yZ091RMEB%2F04vpOzGIXBmFRS4aNvpoGHFOR2svRPlYa1vAZYXcfWcj5ikRLAXpsc%2Ftzi6IixEknP2o%2FPqvqztMvWauhEMqgKXX1Me6E4Ayah\"}],\"group\":\"cf-nel\",\"max_age\":604800}\r\nnel: {\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}\r\nserver: cloudflare\r\ncf-ray: 81c4a8bb0ee302b1-CDG"
}

```

Well, on a simple GET request, though it shouldnt be any different, `stdout` key prints alright.

I notice you didn’t share the whole output and your last debug task should be there, at least in `skipping` status, should the condition not be met.  
You also didn’t indicate the command you’re running your playbook with, so I don’t know what to tell you at this point.

A few improvements, if I may:

- Perhaps consider using [uri module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html) instead of shell; output might be cleaner to parse if needed. I’s also best practice
- I see you’re running your play on localhost, though I haven’t seen you using `local` connection mode (maybe on command line ?); it would not be very optimal to run local command through an ssh connection
- As best practice, consider using [fqcn](https://ansible.readthedocs.io/projects/lint/rules/fqcn/); though that probably won’t change a thing here

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [October 26, 2023, 7:54pm UTC](https://forum.ansible.com/t/parse-the-status-code-and-update-response/1979/5 "2023-10-26T19:54:07Z")

</div>

> Now I have to write a task based on the fields present in the stdout in the response we have to parse stdout whether in stdout are present then it should print as success and if fields are missing then it should print the response as failure.

This isn’t exactly clear, but if I am understanding it correctly, it sounds like your next step would be to take your `mediasvr_resp` registered variable and do some querying/parsing on it. Do some google searches on `ansible json_query`, it is likely your best first starting point.
