Sending JSON variable as a body in curl command

32 views
Skip to first unread message

bikram40

unread,
Mar 15, 2019, 6:56:26 PM3/15/19
to ansible...@googlegroups.com
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Hi All,

I defined a json variable in vars section and want to pass it as body in curl command as below:

vars:

   - paydata: {"status":"complete","message":"successfully   

               completed","actions":["close ticket"]}


- local_action: command curl -X PUT -i -H "Content-Type:application/json" https://ansibledev.corp.xxxx.com:8443/api/job/{{ ticket }} -k --data "{{ paydata }}" completed","actions":["close ticket"]}' --header



But here is the error I am getting. Seems "paydata" is not passed as expected.


fatal: [10.225.240.39 -> localhost]: FAILED! => {"changed": true, "cmd": ["curl", "-X", "PUT", "-i", "-H", "Content-Type:application/json", "https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22", "-k", "-d", "{ustatus:", "ucomplete,", "umessage:", "usuccessfully completed,", "uactions:", "[uclose ticket]}",



Any resolution?



Thank you in advance.


Bikram

Sudheer S

unread,
Mar 15, 2019, 11:25:31 PM3/15/19
to ansible...@googlegroups.com
Why this is marked as abuse? It has been marked as abuse.
Report not abuse


On 16/03/19 4:25 AM, bikram40 wrote:
Hi All,

I defined a json variable in vars section and want to pass it as body in curl command as below:

Have you considered using the uri module instead?



vars:

   - paydata: {"status":"complete","message":"successfully   

               completed","actions":["close ticket"]}


- local_action: command curl -X PUT -i -H "Content-Type:application/json" https://ansibledev.corp.xxxx.com:8443/api/job/{{ ticket }} -k --data "{{ paydata }}" completed","actions":["close ticket"]}' --header



But here is the error I am getting. Seems "paydata" is not passed as expected.


fatal: [10.225.240.39 -> localhost]: FAILED! => {"changed": true, "cmd": ["curl", "-X", "PUT", "-i", "-H", "Content-Type:application/json", "https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22", "-k", "-d", "{ustatus:", "ucomplete,", "umessage:", "usuccessfully completed,", "uactions:", "[uclose ticket]}",



Any resolution?



Please post your playbook. It looks like the data needs proper escaping to be used in the command line.

-

Sudheer S

Kai Stian Olstad

unread,
Mar 16, 2019, 2:21:27 AM3/16/19
to ansible...@googlegroups.com
On 15.03.2019 23:55, bikram40 wrote:
> I defined a json variable in vars section and want to pass it as body in
> curl command as below:

As Sudheer mention you should take a look at the uri module.


>
> vars:
>
> - paydata: {"status":"complete","message":"successfully
>
> completed","actions":["close ticket"]}

This is not json in Ansible it's a dict.


> - local_action: command curl -X PUT -i -H "Content-Type:application/json"
> https://ansibledev.corp.xxxx.com:8443/api/job/{{ ticket }} -k --data "{{
> paydata }}" completed","actions":["close ticket"]}' --header
>
>
>
> But here is the error I am getting. Seems "paydata" is not passed as
> expected.
>
>
> fatal: [10.225.240.39 -> localhost]: FAILED! => {"changed": true, "cmd":
> ["curl", "-X", "PUT", "-i", "-H", "Content-Type:application/json", "
> https://gns-ansible-dev.corp.xxx.com:8443/api/job/CRQmattcheng22", "-k",
> "-d", "{ustatus:", "ucomplete,", "umessage:", "usuccessfully completed,",
> "uactions:", "[uclose ticket]}",

To make a dict to be formated as json you need to use the to_json filter
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data


--
Kai Stian Olstad

bikram40

unread,
Mar 17, 2019, 12:13:47 PM3/17/19
to ansible...@googlegroups.com, Sudheer S, ansible-pr...@olstad.com
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Thanks for your feedback.

But there is a problem in 'URI' module. 
In "URI" module, there is no way to pass "access token". Access token is required here as the url is OAUTH2.0 enabled.

You can get access token by passing client id and secret but you cannot pass it to uri module to get the response back. That's why I am trying to use curl command and sending the JSON body.


Best Wishes,
Bikram

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a096b6cb-4b69-6420-2050-057b98addb1b%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Sudheer Satyanarayana

unread,
Mar 18, 2019, 2:49:47 AM3/18/19
to ansible...@googlegroups.com

On 17/03/19 9:43 PM, bikram40 wrote:
> Thanks for your feedback.
>
> But there is a problem in 'URI' module.
> In "URI" module, there is no way to pass "access token". Access token
> is required here as the url is OAUTH2.0 enabled.
>
> You can get access token by passing client id and secret but you
> cannot pass it to uri module to get the response back. That's why I am
> trying to use curl command and sending the JSON body.

Something seems off here.

I don't see how are you are passing 'access_token' in your curl command
line example.

You can send 'access_token' via URI module in request body, header or URL.

-

Sudheer S


bikram40

unread,
Mar 18, 2019, 2:49:28 PM3/18/19
to ansible...@googlegroups.com, Sudheer S
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Hi Sudheer,

Here is the format of my tasks and it is working perfectly with curl command but I want to send data from variable as I mentioned earlier.


- name: authenticate with restible via oauth2.0

      uri:

        body:

          grant_type: client_credentials

        body_format: form-urlencoded

        force_basic_auth: yes

        method: POST

        user: "{{ client_id }}"

        password: "{{ client_secret}}"

        status_code: 201

        url: https://gns-ansible-dev.corp.xxx.com:8443/auth/token

        validate_certs: no

      register: token_auth_response


    - set_fact: access_token="{{ 'Authorization:'+'Bearer' + ' ' + token_auth_response.json.access_token }}"

    


    - local_action: command curl -X PUT -i -H "Content-Type:application/json" https://gns-ansible-dev.corp.xxx.com:8443/api/job/{{ ticket }} -k --data '{"status":"complete","message":"unsuccessfully completed","actions":["close ticket"]}' --header  "{{ access_token }}"




Thank you very much.


Bikram

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

bikram40

unread,
Mar 18, 2019, 5:42:50 PM3/18/19
to ansible...@googlegroups.com, Sudheer S
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Hi Sudheer,

Sending access token in headers works.
Here is the code section:

headers:

           Authorization: "Bearer {{ token_auth_response.json.access_token }}"

           Content-Type: "application/json"


Thanks for your help.


Bikram

Reply all
Reply to author
Forward
0 new messages