Hi all,
Similar to https://groups.google.com/forum/#!topic/ansible-project/ba5PPAgYGXc but slightly different.
I am trying to use variable substitution like so, putting the variable my_var in the body parameter, which should be a json string:
vars:
my_var: hello_world
tasks:
- uri:
url: https://localhost/api/v1/
method: POST
body: ‘{ “name”: “test”, “var”: “{{ my_var }}”, “location”: “usa” }’
HEADER_Content-Type: “application/json”
status_code: 201
validate_certs: no
user: admin
password: password
I get this error (stacktrace with a TypeError: unhashable type):
TASK: [uri ] ******************************************************************
failed: [localhost] => {“failed”: true, “parsed”: false}
Traceback (most recent call last):
File “/Users/mzb/.ansible/tmp/ansible-tmp-1443729077.78-98124067847188/uri”, line 2067, in
main()
File “/Users/mzb/.ansible/tmp/ansible-tmp-1443729077.78-98124067847188/uri”, line 419, in main
resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout, validate_certs)
File “/Users/mzb/.ansible/tmp/ansible-tmp-1443729077.78-98124067847188/uri”, line 312, in uri
resp, content = h.request(url, method=method, body=body, headers=headers)
File “/Library/Python/2.7/site-packages/httplib2/init.py”, line 1609, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File “/Library/Python/2.7/site-packages/httplib2/init.py”, line 1351, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File “/Library/Python/2.7/site-packages/httplib2/init.py”, line 1273, in _conn_request
conn.request(method, request_uri, body, headers)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 973, in request
self._send_request(method, url, body, headers)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 1007, in _send_request
self.endheaders(body)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 969, in endheaders
self._send_output(message_body)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 833, in _send_output
self.send(message_body)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”, line 805, in send
self.sock.sendall(data)
File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py”, line 229, in sendall
v = self.send(data[count:])
TypeError: unhashable type
If I don’t use quotes around the variable substition, like this:
vars:
my_var: hello_world
tasks:
- uri:
url: https://localhost/api/v1/
method: POST
body: ‘{ “name”: “test”, “var”: {{ my_var }}, “location”: “usa” }’
HEADER_Content-Type: “application/json”
status_code: 201
validate_certs: no
user: admin
password: password
then, I get this error (No JSON type could be decoded):
failed: [localhost] => {“allow”: “GET, POST, HEAD, OPTIONS”, “connection”: “close”, “content”: “{"detail": "JSON parse error - No JSON object could be decoded"}”, “content_type”: “application/json”, “date”: “Thu, 01 Oct 2015 19:50:56 GMT”, “failed”: true, “json”: {“detail”: “JSON parse error - No JSON object could be decoded”}, “redirected”: false, “server”: “Apache/2.4.7 (Ubuntu)”, “status”: 400, “transfer_encoding”: “chunked”, “vary”: “Accept,Cookie”, “x_api_time”: “0.001s”}
msg: Status code was not [201]
Any ideas? Thanks in advance!