Need help on writing a task


  • 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://sfconnect-q.attwifi.com/awss-services/rest/v1/media/rgnet/rxg_gateways?keepFileName=true’ -F ‘file=@/tmp/test_config_1.txt’”

      args:

      warn: no

      register: mediasvr_resp

      ignore_errors: true

    • name: debug the upgrade_response

      debug:

      var: mediasvr_resp
      I tried to create a task for some playbook and response should be like this

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…

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
    ‘’‘’’

    • name: debug the mediasvr_resp
      debug:
      var: mediasvr_resp.stdout

    ‘’’

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

    - debug:
        var: http_code #here is just to show you the code 
    
    - name: update as success
      when: http_code in ['200', '201']
      # the task forwhen it is fine
    
    - name: update as failure
      when: http_code == '500' or (http_code not in ['200', '201', '400', '401'])
     # the task when it is a failure
    

‘’‘’
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…

3 Likes

Hello @Manjunath7

You may try this:

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

"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

2 Likes

You’d have better luck using the ansible.builtin.uri module over trying to parse curl’s CLI output.

2 Likes