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

61 views
Skip to first unread message

Robert Heppe

unread,
Dec 4, 2019, 5:41:41 PM12/4/19
to Ansible Project
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?

---
 - hosts: "{{ region }}-ki01"
   become: true
   tasks:
     - name: Create Kibana Space for client "{{ customer_name }}"
       uri:
         url: "https://{{ region }}-ki01:5601/api/spaces/space"
         user: xxxx
         password: xxxx
         validate_certs: no
         method: POST
         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"]
              }
         body_format: json
         headers:
           kbn-xsrf: "true"
           Content-Type: "application/json"

The output I'm getting right now is this:

[centos@jumphost playbooks]$ ansible-playbook kibana_spaces_test.yml --extra-vars "region=us1 customer_id=some_id customer_name=some_name"

PLAY [us1-ki01] **********************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [us1-ki01]

TASK [Create Kibana Space for client "some_name"] ********************************************************************************************************
fatal: [us1-ki01]: FAILED! => {"changed": false, "content": "", "msg": "Status code was not [200]: Request failed: <urlopen error [Errno -2] Name or service not known>", "redirected": false, "status": -1, "url": "https://us1-ki01:5601/api/spaces/space"}
 [WARNING]: Could not create retry file '/etc/ansible/roles/proficio-ansible/playbooks/kibana_spaces_test.retry'.         [Errno 13] Permission denied:
u'/etc/ansible/roles/proficio-ansible/playbooks/kibana_spaces_test.retry'


PLAY RECAP ***************************************************************************************************************************************************
us1-ki01                   : ok=1    changed=0    unreachable=0    failed=1



Thanks,
Robert

Nick Schendel

unread,
Dec 4, 2019, 6:14:18 PM12/4/19
to Ansible Project
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.

Robert Heppe

unread,
Dec 4, 2019, 7:24:51 PM12/4/19
to Ansible Project
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?

Bram Mertens

unread,
Dec 5, 2019, 1:54:16 AM12/5/19
to ansible...@googlegroups.com
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

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e11f6957-1b70-42bf-a951-2b2595ccf4aa%40googlegroups.com.

Robert Heppe

unread,
Dec 5, 2019, 1:23:26 PM12/5/19
to Ansible Project
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

Nick Schendel

unread,
Dec 6, 2019, 7:39:30 PM12/6/19
to ansible...@googlegroups.com
Sure, I will grab my entire example task in just a bit here.  Happy to share if it might help.

--
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.

Nick Schendel

unread,
Dec 11, 2019, 8:24:56 PM12/11/19
to Ansible Project
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

On Wednesday, December 4, 2019 at 6:24:51 PM UTC-6, Robert Heppe wrote
Reply all
Reply to author
Forward
0 new messages