I'm trying to do something which I thought would be fairly straightforward, but I've run into a number of bugs/roadblocks and so I'm coming to the community for some help solving my problem.
What I'm trying to do is this: create a JSON request body and send a URI request to a server. Here's a slimmed-down version of my playbook:
---
- hosts: 127.0.0.1
connection: local
vars:
cpu_count: 3
bar: "whooo"
bar2: "wheee"
tasks:
- set_fact:
request_body: {
"cpus": "{{cpu_count}}",
"mem": 1500,
"env": {
"FOO": "{{bar}}",
"FOO2": "{{bar2}}",
},
"constraints": [["123", "456"]]
}
- name: Kick off app
method=PUT
body='{{ request_body | to_json }}'
HEADER_Content-Type="application/json"
status_code=200,201,204
{"mem": 1500, "cpus": "3", "env": {"FOO": "whooo", "FOO2": "wheee"}, "constraints": [["123", "456"]]}The problem with this is that I don't want the "cpus" variable quoted - it's an integer, and my actual target server rejects it. I believe this is a known issue (https://github.com/ansible/ansible/issues/9362). And no, {{cpu_count | int}} doesn't help. It still quotes the value in the resulting JSON. In an attempt to work around this, I tried doing something like this:
- set_fact:
request_string: '{{ request_body | to_json | regex_replace("cpus.: .([0-9\\.]+).", "cpus\": \\1") }}'
- name: Kick off app
uri: url=http://requestb.in/1laafhn1
method=PUT
body='{{ request_string }}'
HEADER_Content-Type="application/json"
status_code=200,201,204
This has the (somewhat strange to me) effect of stripping all quotes from the value PUT on the server:
{mem: 1500, cpus: 3, env: {FOO: whooo, FOO2: wheee}, constraints: [[123, 456]]}And changing the single quotes in the URI module to double quotes makes my JSON quoted with single quotes. Also not helpful.
Lastly, I've tried to use the URI module with YAML-style output:
- name: Kick off app
uri:
url: http://requestb.in/1laafhn1
method: PUT
body: '{{ request_string }}'
HEADER_Content-Type: "application/json"
status_code: 200,201,204
This results in an ansible stack trace, which appears to be this bug: https://github.com/ansible/ansible-modules-core/issues/265
If you've read this far - I would love some alternative suggestions on how to accomplish this using Ansible. Thank you in advance!