Need help using URI module for a curl command with json body

Hi all,
I have a working curl command that I’ve been trying to get into an ansible playbook in a testing environment. I’ve found several posts on various forums for similar situations, but not the one I’m working with, so my attempts are still erroring. I have the curl below, which when run on the cli works correctly.

curl -X POST -k -u xxxx:xxxx “https://x.x.x.x:5601/api/spaces/space” -H ‘kbn-xsrf: true’ -H ‘Content-Type: application/json’ -d’
{
“id”: “some_id”,
“name”: “some_name”,
“disabledFeatures”: [“visualize”,“dev_tools”,“advancedSettings”,“indexPatterns”,“savedObjectsManagement”,“timelion”,“graph”,“monitoring”,“ml”,“apm”,“maps”,“canvas”,“infrastructure”,“logs”,“siem”,“uptime”]
}

However, I have tried many variations of it in a playbook without success. Can anyone suggestions modifications to this playbook?

Have you tried the body section with the the curly braces? Like so:

body:

“id”: “{{ customer_id }}”,
“name”: “{{ customer_name }}”,
“disabledFeatures”: [“visualize”,“dev_tools”,“advancedSettings”,“indexPatterns”,“savedObjectsManagement”,“timelion”,“graph”,“monitoring”,“ml”,“apm”,“maps”,“canvas”,“infrastructure”,“logs”,“siem”,“uptime”]

I have a playbook using URI module with a json body and mine works without those.

Thanks Nick,

I have not. I just tried this but get another error for the , at the end of the line. So I removed them and it errors again.

body:
“id”: “{{ customer_id }}”
“name”: “{{ customer_name }}”
“disabledFeatures”: [“visualize”,“dev_tools”,“advancedSettings”,“indexPatterns”,“savedObjectsManagement”,“timelion”,“graph”,“monitoring”,“ml”,“apm”,“maps”,“canvas”,“infrastructure”,“logs”,“siem”,“uptime”]

Could you post your entire uri block as an example and I could try to modify mine to match the syntax?

Hi,

Perhaps it is only a problem in your example but in the example you appear to use an IP address for by a port. (x.x.x.x:5601) but in the playbook you seem to use a hostname.

Could it be a name resolution issue?
Does it work if you hardcode the exact same URL in the playbook?

Another thing I noticed is that the URL in the playbook has a suffix of -ki01 (url: “https://{{ region }}-ki01:5601/api/spaces/space”).
Is that intentional?

Hth

Bram

Hi Bram,
Yes, I just removed the IP for the post. I think connectivity should be fine as I can telnet to the hostname from the ansible system.

[centos@jumphost ~]$ telnet us1-ki01 5601
Trying x.x.101.185…
Connected to us1-ki01.
Escape character is ‘^]’.
^]

I wonder if I have the double headers entered correctly?

headers:
kbn-xsrf: “true”
Content-Type: “application/json”

Thanks,
Robert

Sure, I will grab my entire example task in just a bit here. Happy to share if it might help.

Sorry for the delay, I meant to grab the example when I got back to my desk and lost track of it.
Here is an example I am using currently that works

  • name: restore disk from snapshot
    uri:
    url: https://{{ cluser_ip }}/api/internal/vmware/vm/snapshot/{{snapshotlist.json.data[0].id}}/mount_disks
    headers:
    Content-Type: “application/json”
    Authorization: “Bearer {{ authtoken.json.token }}” # pass authentication token from earlier rather than basic auth
    body:
    targetVmId: ‘{{ target_vm_id }}’ # VM to restore the disk snapshots to
    vmdkIds: [‘{{ snapshotdetails.json.snapshotDiskDetails[1].virtualDiskId }}’] # Rubrik wants an array of vmdk ID’s to restore, without it will restore all in the snapshot
    body_format: json
    method: POST
    status_code: 202 # Rubrik returns 202 on sucess.
    return_content: yes
    validate_certs: yes
    tags: restore
    delegate_to: localhost
    become: no