Sending JSON through URI module

574 views
Skip to first unread message

Junaid Farooq

unread,
Dec 11, 2019, 2:28:43 PM12/11/19
to Ansible Project
I am doing this task

- name: Register evercam-media local commit hash
  local_action
:
    shell
    chdir
={{local_media_dir}}
    git log
--pretty='{"sha":"%h", "author_name":"%an", "author_name":"%ae", "unix_timestamp":"%at", "subject":"%s", "branch":"%d"}' | head -1
 
register: local_commit_info


- set_fact:
    commit
: "{{ local_commit_info.stdout }}"


which creates such as

changed: [media.evercam.io -> localhost] => {
   
"changed": true,
   
"cmd": "git log --pretty='{\"sha\":\"%h\", \"author_name\":\"%an\", \"author_name\":\"%ae\", \"unix_timestamp\":\"%at\", \"subject\":\"%s\", \"branch\":\"%d\"}' | head -1",
   
"delta": "0:00:00.005962",
   
"end": "2019-12-11 19:24:54.788804",
   
"invocation": {
       
"module_args": {
           
"_raw_params": "git log --pretty='{\"sha\":\"%h\", \"author_name\":\"%an\", \"author_name\":\"%ae\", \"unix_timestamp\":\"%at\", \"subject\":\"%s\", \"branch\":\"%d\"}' | head -1",
           
"_uses_shell": true,
           
"argv": null,
           
"chdir": "/home/junaid/evercam/evercam-server",
           
"creates": null,
           
"executable": null,
           
"removes": null,
           
"stdin": null,
           
"stdin_add_newline": true,
           
"strip_empty_ends": true,
           
"warn": true
       
}
   
},
   
"rc": 0,
   
"start": "2019-12-11 19:24:54.782842",
   
"stderr": "",
   
"stderr_lines": [],
   
"stdout": "{\"sha\":\"aca4ef2\", \"author_name\":\"Junaid Farooq\", \"author_name\":\"jun...@evercam.io\", \"unix_timestamp\":\"1576042588\", \"subject\":\"Merge pull request #1240 from evercam/delete_timelapse\", \"branch\":\" (HEAD -> master, origin/master, origin/HEAD)\"}",
   
"stdout_lines": [
       
"{\"sha\":\"aca4ef2\", \"author_name\":\"Junaid Farooq\", \"author_name\":\"jun...@evercam.io\", \"unix_timestamp\":\"1576042588\", \"subject\":\"Merge pull request #1240 from evercam/delete_timelapse\", \"branch\":\" (HEAD -> master, origin/master, origin/HEAD)\"}"
   
]
}
Read vars_file 'common-vars.yml'
Read vars_file 'media-vars.yml'
Read vars_file '../private_vars_media.yml'


TASK
[media : set_fact] *******************************************************************************************************************************************************************************************
task path
: /home/junaid/evercam/evercam-devops/ansible/media/tasks/hotcode.yml:8
ok
: [media.evercam.io] => {
   
"ansible_facts": {
       
"commit": {
           
"author_name": "jun...@evercam.io",
           
"branch": " (HEAD -> master, origin/master, origin/HEAD)",
           
"sha": "aca4ef2",
           
"subject": "Merge pull request #1240 from evercam/delete_timelapse",
           
"unix_timestamp": "1576042588"
       
}
   
},
   
"changed": false
}
All I want to do is to send the commit to a URL as a post request.

I am doing this way

- uri:
    url
: "http://www.myapi.com/create"
    method
: POST
    return_content
: yes
    body_format
: json
    HEADER_Content
-Type: "application/json"
    body
: "{{ commit }}"

But I am getting error such as

fatal: [media.evercam.io]: FAILED! => {
   
"changed": false,
   
"invocation": {
       
"module_args": {
           
"HEADER_Content-Type": "application/json",
           
"body": {
               
"author_name": "jun...@evercam.io",
               
"branch": " (HEAD -> master, origin/master, origin/HEAD)",
               
"sha": "aca4ef2",
               
"subject": "Merge pull request #1240 from evercam/delete_timelapse",
               
"unix_timestamp": "1576042588"
           
},
           
"body_format": "json",
           
"method": "POST",
           
"return_content": true,
           
"url": "http://www.myapi.com/create"
       
}
   
},
   
"msg": "Unsupported parameters for (uri) module: HEADER_Content-Type Supported parameters include: attributes, backup, body, body_format, client_cert, client_key, content, creates, delimiter, dest, directory_mode, follow, follow_redirects, force, force_basic_auth, group, headers, http_agent, method, mode, owner, regexp, remote_src, removes, return_content, selevel, serole, setype, seuser, src, status_code, timeout, unix_socket, unsafe_writes, url, url_password, url_username, use_proxy, validate_certs"
}



Can you point where I am wrong? I also tried Just using curl which didn't work as well?

Junaid Farooq

unread,
Dec 11, 2019, 2:52:13 PM12/11/19
to Ansible Project
I have removed headers but now I am getting this error

fatal: [media.evercam.io]: FAILED! => {
   
"changed": false,

   
"content": "",
   
"elapsed": 0,
   
"invocation": {
       
"module_args": {
           
"attributes": null,
           
"backup": null,
           
"body": {

               
"commit": {
                   
"author_name": "jun...@evercam.io",
                   
"branch": " (HEAD -> master, origin/master, origin/HEAD)",
                   
"sha": "aca4ef2",
                   
"subject": "Merge pull request #1240 from evercam/delete_timelapse",
                   
"unix_timestamp": "1576042588"
               
}
           
},

           
"body_format": "json",
           
"client_cert": null,
           
"client_key": null,
           
"content": null,
           
"creates": null,
           
"delimiter": null,
           
"dest": null,
           
"directory_mode": null,
           
"follow": false,
           
"follow_redirects": "safe",
           
"force": false,
           
"force_basic_auth": false,
           
"group": null,
           
"headers": {
               
"Content-Type": "application/json"
           
},
           
"http_agent": "ansible-httpget",
           
"method": "POST",
           
"mode": null,
           
"owner": null,
           
"regexp": null,
           
"remote_src": null,
           
"removes": null,
           
"return_content": true,
           
"selevel": null,
           
"serole": null,
           
"setype": null,
           
"seuser": null,
           
"src": null,
           
"status_code": [
               
200
           
],
           
"timeout": 30,
           
"unix_socket": null,
           
"unsafe_writes": null,
           
"url": "http://127.0.0.1:3000/v1/server/deployed",
           
"url_password": null,
           
"url_username": null,
           
"use_proxy": true,
           
"validate_certs": true
       
}
   
},
   
"msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>",
   
"redirected": false,
   
"status": -1,
   
"url": "http://127.0.0.1:3000/v1/server/deployed"
}


   
"stdout": "{\"sha\":\"aca4ef2\", \"author_name\":\"Junaid Farooq\", \"author_name\":\"junaid@evercam.io\", \"unix_timestamp\":\"1576042588\", \"subject\":\"Merge pull request #1240 from evercam/delete_timelapse\", \"branch\":\" (HEAD -> master, origin/master, origin/HEAD)\"}",
   
"stdout_lines": [
       
"{\"sha\":\"aca4ef2\", \"author_name\":\"Junaid Farooq\", \"author_name\":\"junaid@evercam.io\", \"unix_timestamp\":\"1576042588\", \"subject\":\"Merge pull request #1240 from evercam/delete_timelapse\", \"branch\":\" (HEAD -> master, origin/master, origin/HEAD)\"}"

Abhijeet Kasurde

unread,
Dec 11, 2019, 8:23:30 PM12/11/19
to Ansible Project
 Hi Junaid,

You can convert stdout to json using `jq` binary and `from_json` filter like -

  tasks:
    - name: local
      local_action:
        shell
        chdir=/Volumes/data/src/ansible/
        git log --pretty='["%h", "%an", "%ae", "%ad", "%s"]' | head -1 | jq
      register: local_commit_info

    - set_fact:
        m: "{{ local_commit_info.stdout | from_json }}"

    - debug:
        msg: "{{ m[1] }}"

You can `m` directly in your next call.


Reply all
Reply to author
Forward
0 new messages