# Need help on writing a task

**URL:** <https://forum.ansible.com/t/need-help-on-writing-a-task/1939>\
**Category:** Get Help\
**Tags:** playbook, ansible-core\
**Created:** [October 25, 2023, 6:50am UTC](https://forum.ansible.com/t/need-help-on-writing-a-task/1939 "2023-10-25T06:50:46Z")\
**Posts on this page:** 4\
**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 25, 2023, 6:50am UTC](https://forum.ansible.com/t/need-help-on-writing-a-task/1939/1 "2023-10-25T06:50:46Z")

</div>

* * *

- name: Get media\_server response

ok: [localhost] =\> {

```
"msg": {

    "changed": true,

    "cmd": "curl -X PUT -H 'Username: AWSSUsername1' -H 'Password: AWSSPassword1' 'https://sfconnect-q.attwifi.com/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\"}"

    ]

}

```

}

but in my output I’m missing the fields in “stdout” I have to get those fields in my response then only successful or else no, and also I have to create one more task for this with below conditions

# task2: update status to cmdb

# if 200,201, - update as success

# if 400,401 or job failure - dont update any to cmdb

# if 500 or others - update as failure

Can someone help me it is bit urgent…

---

<div class="post-metadata">

**Author:** ![valkiriaaquatica](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/valkiriaaquatica/32/1308_2.png) [@valkiriaaquatica](https://forum.ansible.com/u/valkiriaaquatica)\
**Post date:** [October 25, 2023, 12:12pm UTC](https://forum.ansible.com/t/need-help-on-writing-a-task/1939/2 "2023-10-25T12:12:20Z")

</div>

Hi @Manjunath7 nice to meet you!

I have read and i understand there are two main tasks right?

- First one–\> Is the one you just did, you can add the debud of the stdout of the respondse from the curl you register in mediasvr\_resp.  
Example  
‘’‘’’

1. Second task, checking the code we recieve from thecurl request

#First we check if the rc == 0 of the curl request and then depending on the #http\_code you assume it was fine or it wans’fine  
‘’’

- name: Update status to CMDB  
when: mediasvr\_resp.rc == 0  
block:  
- set\_fact:  
http\_code: “{{ mediasvr\_resp.stdout → #put some regex to get the http code  
or maybe the stdout gives you some lebal of the json qwhere you can catch the  
htt p code}}”

‘’‘’  
My reocmmendation is for this http requests you can better send the values to a go or python scriptt you call from ansible, using command: python3 {{ script }} --values and then register theoutput of the script, is much easer to manipulate data using python,golang… You can easily check the response, filter data…

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [October 25, 2023, 12:23pm UTC](https://forum.ansible.com/t/need-help-on-writing-a-task/1939/3 "2023-10-25T12:23:52Z")

</div>

Hello @Manjunath7

You may try this:

```yml
---
- name: Get media_server response
  hosts: localhost
  gather_facts: no
  tasks:

    - name: Pushing the generated configuration file to Media Server (sfconnect)
      ansible.builtin.command:
        cmd: >-
          curl 'https://sfconnect-q.attwifi.com/awss-services/rest/v1/media/rgnet/rxg_gateways?keepFileName=true'
          -X PUT
          -H 'Username: AWSSUsername1'
          -H 'Password: AWSSPassword1'
          -F 'file=@/tmp/test_config_1.txt'
          -w '%{http_code}'
      register: mediasvr_resp
      ignore_errors: true

    - name: debug the upgrade_response
      ansible.builtin.debug:
        var: mediasvr_resp

    - name: Extract HTTP response
      ansible.builtin.set_fact:
        http_code: "{{ mediasvr_resp.stdout | regex_search('([0-9]+)$') }}"

    - name: Show HTTP response
      ansible.builtin.debug:
        var: http_code

    - name: show message if HTTP response == 200
      ansible.builtin.debug:
        msg: "everything is OK -> HTTP RESPONSE {{ http_code }}"
      when: http_code == '200'
...

```

It’s a bit hacky, but does the job anyway. Beware that I’m not very good at regex, you might want to review it.

The -w argument in curl gives the http response code on the return, but in a total messy way… that’s why I used the regex 😛

```auto
"stdout": "{\"fileName\": \"test_config_1.txt\", \"path\": \"/rgnet/rxg_gateways\", \"bytes\": \"9\", \"mimeType\": \"application/octet-stream\", \"modified\": \"Wed Oct 25 12:20:25 UTC 2023\"}200"

```

(Notice the http status code at the end of the string)

See if the fix it suits you

Cheers!

PS: One suggestion; use the `ansible.builtin.command` instead of `ansible.builtin.shell`, I believe is the way to go these days

---

<div class="post-metadata">

**Author:** ![gotmax23](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gotmax23/32/106_2.png) [@gotmax23](https://forum.ansible.com/u/gotmax23)\
**Post date:** [October 25, 2023, 4:26pm UTC](https://forum.ansible.com/t/need-help-on-writing-a-task/1939/4 "2023-10-25T16:26:22Z")

</div>

You’d have better luck using the [`ansible.builtin.uri`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html) module over trying to parse curl’s CLI output.
