Ansible unable to except any other body format except raw and jason in the uri module and upload a zip or a jar via POST method

Zip file not getting POST to the server with the uri module in Ansible.

These are the codes I am trying to execute

  • name: Transfering the Proxies
    uri:
    headers:
    Authorization: ‘{{API_USER_PASS_BASE64_ENCRYPT}}’
    Content-Type: “application/octet-stream”
    url: https://api.enterprise.apigee.com/v1/organizations/{{API_ORG}}/apis?action=import&name={{proxy_name }}-{{API_ENVIRONMENT}}
    #body: “{{ lookup(‘file’,‘{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}–hughes-dev-test-dev.zip’) }}” → doesn’t work
    src: /{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}–hughes-dev-test-dev.zip
    body_format: raw
    status_code: 201, 200
    method: POST
    register: version

It gives the following error:
“json”: {
“code”: “messaging.config.beans.InvalidBundle”,
“contexts”: ,
“message”: “Bundle is invalid. Empty bundle”
},

Also it sets the “content_type”: “application/json”, at the top of the message like this:

The full traceback is:
File “/tmp/ansible_NyKoMf/ansible_module_uri.py”, line 468, in main
uresp[‘location’] = absolute_location(url, uresp[‘location’])

fatal: [localhost]: FAILED! => {
“changed”: false,
“connection”: “Close”,
“content”: “{\n “code” : “messaging.config.beans.InvalidBundle”,\n “message” : “Bundle is invalid. Empty bundle”,\n “contexts” : \n}”,
“content_length”: “122”,
“content_type”: “application/json”,
“date”: “Mon, 14 May 2018 19:03:48 GMT”,
“invocation”: {
“module_args”: {
“attributes”: null,
“backup”: null,
“body”: null,
“body_format”: “raw”,
“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”: {
“Authorization”: “Basic YXBvb3J2YS5tb25kYWxAaHVnaGVzLmNvbTphcG9vcnZhNw==”,
“Content-Type”: “application/octet-stream”
},
“http_agent”: “ansible-httpget”,
“method”: “POST”,
“mode”: null,
“owner”: null,
“regexp”: null,
“remote_src”: null,
“removes”: null,
“return_content”: false,
“selevel”: null,
“serole”: null,
“setype”: null,
“seuser”: null,
“src”: “/var/tmp/APIGEE_COM_R001_B-001_P-001/apigee/proxies/FapProxy–hughes-dev-test-dev.zip”,
“status_code”: [
“201”,
" 200"
],
“timeout”: 30,
“unsafe_writes”: null,
“url”: “https://api.enterprise.apigee.com/v1/organizations/hughes-dev/apis?action=import&name=FapProxy-sa-dev”,
“url_password”: null,
“url_username”: null,
“use_proxy”: true,
“validate_certs”: true
}
},
“json”: {
“code”: “messaging.config.beans.InvalidBundle”,
“contexts”: ,
“message”: “Bundle is invalid. Empty bundle”
},
“msg”: “Status code was not [201, 200]: HTTP Error 400: Bad Request”,
“redirected”: false,
“server”: “Apigee LB”,
“status”: 400,
“url”: “https://api.enterprise.apigee.com/v1/organizations/hughes-dev/apis?action=import&name=FapProxy-sa-dev
}

    • name: Transfering the Proxies
  uri:
    headers:
     Authorization: '{{API_USER_PASS_BASE64_ENCRYPT}}'
     Content-Type: "application/zip"
    url: https://api.enterprise.apigee.com/v1/organizations/{{API_ORG}}/apis?action=import&name={{ proxy_name }}-{{API_ENVIRONMENT}}
    body: "{{ lookup('file','{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip') }}"
    #src: '{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip'
    #body_format: raw
    status_code: 201, 200
    method: POST
  register: version

Please check and provide information on how to properly POST a zip file.

ISSUE TYPE- Bug Report
  • Feature Idea
  • Documentation Report
COMPONENT NAME

URI Module

ANSIBLE VERSION
ansible 2.4.2.0

CONFIGURATION##### OS / ENVIRONMENT

Red Hat Enterprise Linux Server 7.3 (Maipo)

STEPS TO REPRODUCE
- name: Transfering the Proxies
  uri:
    headers:
     Authorization: '{{API_USER_PASS_BASE64_ENCRYPT}}'
     Content-Type: "application/zip"
    url: https://api.enterprise.apigee.com/v1/organizations/{{API_ORG}}/apis?action=import&name={{ proxy_name }}-{{API_ENVIRONMENT}}
    body: "{{ lookup('file','{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip') }}"
    #src: '{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip'
    #body_format: raw
    status_code: 201, 200
    method: POST
  register: version

---
- name: Transfering the Proxies
  uri:
    headers:
     Authorization: '{{API_USER_PASS_BASE64_ENCRYPT}}'
     Content-Type: "multipart/form-data"
    url: https://api.enterprise.apigee.com/v1/organizations/{{API_ORG}}/apis?action=import&name={{ proxy_name }}-{{API_ENVIRONMENT}}
    #body: "{{ lookup('file','{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip') }}"
    src: '{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip'
    #body_format: raw
    status_code: 201, 200
    method: POST
  register: version

- name: Transfering the Proxies
  uri:
    headers:
     Authorization: '{{API_USER_PASS_BASE64_ENCRYPT}}'
     Content-Type: "application/octet-stream"
    url: https://api.enterprise.apigee.com/v1/organizations/{{API_ORG}}/apis?action=import&name={{ proxy_name }}-{{API_ENVIRONMENT}}
    #body: "{{ lookup('file','{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip') }}" --> doesn't work
    src: /{{ path }}/{{bundle_name}}/apigee/proxies/{{proxy_name}}--hughes-dev-test-dev.zip
    body_format: raw
    status_code: 201, 200
    method: POST
  register: version

EXPECTED RESULTS

The zip file should be uploaded to the server.

ACTUAL RESULTS

Right now as uri module is not working I am using the curl command instead.
But I want my code not to have dependency on the linux commands. Hence trying to use the uri module.

  • name: Transfering the Proxies
    command: ‘curl -X POST --header “Authorization: Basic YXBvb3J2YS5tb25kYWxAaHVnaGVzLmNvbTphcG9vcnZhNw==” -F “file=@/{{path}}/{{bundle_name}}/apigee/proxies/{{proxy_name}}–hughes-dev-test-dev.zip” “https://api.enterprise.apigee.com/v1/organizations/{{API_ORG}}/apis?action=import&name={{proxy_name }}-{{API_ENVIRONMENT}}”’
    register: version
    Neither src nor body parameter is able to POST a zip or jar file to the server.

I am facing exactly the same issue when trying to upload a jar file over a Flink REST endpoint.

I tried both with curl and postman, it works flawlessly, but using Ansible URI module, I am getting the error message: FAILED! => {“changed”: false, “content”: “”, “msg”: “Status code was -1 and not [200]: Request failed: <urlopen error [Errno 104] Connection reset by peer>”, “redirected”: false, “status”: -1, “url”: "http://:8081/jars/upload}

Any idea, or should I continue using the shell module with curl (I don’t like it, but it’s the only way it works for now).

What does your task look like?

Also, the original report is against 2.4.2 which by now is almost 1.5 years old, and the uri module has since seen a lot of changes.
What version do you run?

Dick