Parse the status code and update response

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

---

- 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 :

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.

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?

1 Like

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

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:

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:

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 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; though that probably won’t change a thing here
3 Likes

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.

3 Likes