0
votes

If I add a body to a uri: task the task fails with "Status code was -1 and not [200]: An unknown error occurred: unhashable type"

If I remove the body: then the Web service returns an error (because it needs the body!) but at least it gets as far as the webservice!

This is the task:

  - name: create new RE if RE doesn't exist
    uri:
      url: https://api.eu.cloud.talend.com/tmc/v1.3/runtimes/remote-engines
      method: POST
      headers:
        Authorization: Basic {{talend_personal_access_token}}
        Accept: application/json
      status-code: 201
      body-format: json
      body:
        name: "{{RE_name}}"
    register: new_RE_response
    when: check_response.json[0].id is undefined

This is the output:

{
    "status": -1,
    "exception": "Traceback (most recent call last):\n  File \"/tmp/ansible_uri_payload_nEptqB/ansible_uri_payload.zip/ansible/module_utils/urls.py\", line 1359, in fetch_url\n    unix_socket=unix_socket)\n  File \"/tmp/ansible_uri_payload_nEptqB/ansible_uri_payload.zip/ansible/module_utils/urls.py\", line 1257, in open_url\n    use_gssapi=use_gssapi, unix_socket=unix_socket)\n  File \"/tmp/ansible_uri_payload_nEptqB/ansible_uri_payload.zip/ansible/module_utils/urls.py\", line 1163, in open\n    r = urllib_request.urlopen(*urlopen_args)\n  File \"/usr/lib64/python2.7/urllib2.py\", line 154, in urlopen\n    return opener.open(url, data, timeout)\n  File \"/usr/lib64/python2.7/urllib2.py\", line 431, in open\n    response = self._open(req, data)\n  File \"/usr/lib64/python2.7/urllib2.py\", line 449, in _open\n    '_open', req)\n  File \"/usr/lib64/python2.7/urllib2.py\", line 409, in _call_chain\n    result = func(*args)\n  File \"/tmp/ansible_uri_payload_nEptqB/ansible_uri_payload.zip/ansible/module_utils/urls.py\", line 356, in https_open\n    return self.do_open(CustomHTTPSConnection, req)\n  File \"/usr/lib64/python2.7/urllib2.py\", line 1211, in do_open\n    h.request(req.get_method(), req.get_selector(), req.data, headers)\n  File \"/usr/lib64/python2.7/httplib.py\", line 1056, in request\n    self._send_request(method, url, body, headers)\n  File \"/usr/lib64/python2.7/httplib.py\", line 1090, in _send_request\n    self.endheaders(body)\n  File \"/usr/lib64/python2.7/httplib.py\", line 1052, in endheaders\n    self._send_output(message_body)\n  File \"/usr/lib64/python2.7/httplib.py\", line 894, in _send_output\n    self.send(message_body)\n  File \"/usr/lib64/python2.7/httplib.py\", line 866, in send\n    self.sock.sendall(data)\n  File \"/usr/lib64/python2.7/ssl.py\", line 744, in sendall\n    v = self.send(data[count:])\nTypeError: unhashable type\n",
    "url": "https://api.eu.cloud.talend.com/tmc/v1.3/runtimes/remote-engines",
    "changed": false,
    "elapsed": 1,
    "content": "",
    "invocation": {
        "module_args": {
            "directory_mode": null,
            "force": false,
            "remote_src": null,
            "status_code": [
                200
            ],
            "follow": false,
            "owner": null,
            "body_format": "raw",
            "body-format": "json",
            "client_key": null,
            "group": null,
            "use_proxy": true,
            "headers": {
                "Accept": "application/json",
                "Authorization": "Basic <censored>"
            },
            "unsafe_writes": null,
            "setype": null,
            "content": null,
            "serole": null,
            "follow_redirects": "safe",
            "return_content": false,
            "client_cert": null,
            "body": {
                "name": "RHEL_X.X.X.X_AZGLGBNEVTA30_PRODUCTION_C4E"
            },
            "timeout": 30,
            "src": null,
            "dest": null,
            "selevel": null,
            "force_basic_auth": true,
            "removes": null,
            "http_agent": "ansible-httpget",
            "regexp": null,
            "status-code": 201,
            "url_password": null,
            "url": "https://api.eu.cloud.talend.com/tmc/v1.3/runtimes/remote-engines",
            "validate_certs": true,
            "seuser": null,
            "method": "POST",
            "creates": null,
            "unix_socket": null,
            "delimiter": null,
            "mode": null,
            "url_username": null,
            "attributes": null,
            "backup": null
        }
    },
    "redirected": false,
    "msg": "Status code was -1 and not [200]: An unknown error occurred: unhashable type",
    "_ansible_no_log": false
}

This is the Ansible version:

ansible --version
ansible 2.9.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/jason_ho/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jun 11 2019, 14:33:56) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

This is running through AWX 9.1.1

Is it a bug or something else I'm doing wrong?

2
As noted below, you need to change 'status-code: 201' to 'status_code: 201'. Having that parameter spelled incorrectly would use the defaults - and "200" is the default as you mentioned below. When you do fix this, does the playbook work as expected now? - dan_linder

2 Answers

0
votes

Your syntax is incorrect. The correct variable is status_code, not status-code.

(An "underline" and not a "dash": https://docs.ansible.com/ansible/latest/modules/uri_module.html)

0
votes

First of all you are using status code 201 and expecting status code to be 200 success. Remove status code if you are expecting 200 success response.
Second of all, use return_content: true if you are sending a POST request.

For more info. check https://docs.ansible.com/ansible/latest/modules/uri_module.html#uri-interacts-with-webservices