Ansible uri body bug

Published on:

So there's a very annoying Ansible bug related to the uri body. Let's say you have something like this.

- uri:
    url: "http://some.url"
    method: PUT
    body: '{"url": "http://{{ ansible_eth0.ipv4.address }}:3003"}'
    body_format: json

No, you didn't do anything wrong, but you will get a nasty error: TypeError: must be string or buffer, not dict. You can read about the details here: https://github.com/ansible/ansible-modules-core/issues/265

This hack will work around the problem. Essentially you have to add a null value to your JSON so that Ansible won't wrongly turn the JSON string back into a dict.

vars:
  listener_body:
    url: "http://{{ ansible_eth0.ipv4.address }}:3003"
    _hack: null
tasks:
  - uri:
    url: "http://some.url"
    method: PUT
    body: "{{ listener_body | to_json }}"
    body_format: json