Variable in "body" parameter of "uri" module

Hi all!

This is my task that works:

`

  • name: Stop services on ambari
    uri:
    url: http://{{ inventory_hostname }}:8080/api/v1/clusters/{{ hostvars[inventory_hostname].cluster_name }}/services/{{ service }}
    method: PUT
    force_basic_auth: yes
    user: “{{ ambari_admin_user }}”
    password: “{{ ambari_admin_password }}”
    headers: ‘{“X-Requested-By”:“ambari”}’
    body: “{"RequestInfo":{ "context":"Stop services via REST"},"ServiceInfo":{"state":"INSTALLED"}}”
    status_code: 200,201,202
    body_format: raw
    timeout: 120
    return_content: yes
    register: returned_from_cluster
    when: “‘AMBARI-SERVER’ in group_names”
    `

I want to change the Stop services via REST part to Stop {{ service }} via REST.
But the playbook fault with:
"msg": "Status code was -1 and not [200, 201, 202]: An unknown error occurred: must be string or buffer, not dict"

I tried to run the playbook with -vvv and saw the diffenence.

This one without variable:

"body": "{\\"RequestInfo\\":{ \\"context\\":\\"Stop services via REST\\"},\\"ServiceInfo\\":{\\"state\\":\\"INSTALLED\\"}}"

And this one with variable:

"body": {"RequestInfo": {"context": "Stop NIFI_REGISTRY via REST"}, "ServiceInfo": {"state": "INSTALLED"}}

How can I avoid this evaluation?

Ok, I have found a solution.
First of all, for some bug in Ambari I have to use raw body format and no json.
So I have changed the body_format to json and appended “Content-Type”:“text/plain” to the headers parameter.
Now my headers parameter looks like:

`
headers: ‘{“X-Requested-By”:“ambari”, “Content-Type”:“text/plain”}’

`
Now everything works properly.