Category Archives: Bugs

Workaround for a uri module issue in Ansible

Its worth noting that in the current version of Ansible (2.6.3-1) as of writing contains a bug (https://github.com/ansible/ansible/issues/37182) in relation to support for content type x-www-form-urlencoded, which will be fixed in the Ansible 2.7 release.  There is a workaround for this bug though, by setting the body_format to raw and then passing the body as form encoded string itself works.
Lets use an example calling keycloak for an authentication token. The keycloak REST API expects an x-www-form-urlencoded string containing the credentials to authenticate against:
- uri:
    url: "{{ keycloak_server_url }}"
    method: POST
    body: "username={{ keycloak_username }}&password={{ keycloak_password }}&grant_type={{keycloak_grant_type }}&client_id={{keycloak_client_id }}"
    headers:
      Content-Type: "application/x-www-form-urlencoded"
    validate_certs: no
  register: response
In this task, the body will be passed to the API as-is without the module trying to convert the body contents into JSON. The bug fix should make what is passed through to the API clearer though.