Question:
I want to make a POST request to an API endpoint via Ansible where some of the items inside the post data are dynamic, here is what I try and fail:
My body_content.json:
1 2 3 4 5 6 |
{ apiKey: '{{ KEY_FROM_VARS }}', data1: 'foo', data2: 'bar' } |
And here is my Ansible task:
1 2 3 4 5 |
# Create an item via API - uri: url="http://www.myapi.com/create" method=POST return_content=yes HEADER_Content-Type="application/json" body="{{ lookup('file','create_body.json') | to_json }}" |
Sadly this doesn’t work:
1 2 3 4 5 |
failed: [localhost] => {"failed": true} msg: this module requires key=value arguments .... FATAL: all hosts have already failed -- aborting |
My ansible version is 1.9.1
Answer:
You can’t use newlines like this in yaml. Try this instead (the “>” indicates that the next lines are to be concatenated):
1 2 3 4 5 6 |
# Create an item via API - uri: > url="http://www.myapi.com/create" method=POST return_content=yes HEADER_Content-Type="application/json" body="{{ lookup('file','create_body.json') | to_json }}" |
But I find this much better:
1 2 3 4 5 6 7 8 |
# Create an item via API - uri: url: "http://www.myapi.com/create" method: POST return_content: yes HEADER_Content-Type: "application/json" body: "{{ lookup('file','create_body.json') | to_json }}" |