J. Doe
J. Doe

Reputation: 1529

Error with Ansible URI module and variables

I have the following code -

- name: Create a repo
  uri:
    url: http://mystash.com/stash/rest/api/1.0/projects/PROJECT/repos/
    method: POST
    body: '{"name":"{{ somevar }}_settings"}'
    force_basic_auth: yes 
    status_code: 201 
    headers:
      Content-Type: "application/json"
      Authorization: "Basic bm90bXlwYXNzd29yZA==="
      Accept: "application/json"

The issue that I am having is when I try to run this, I get this error -

 An unknown error occurred: sendall() argument 1 must be string or buffer, not dict"

Is there a way to set the variable that I am passing to it as a string inside of the ansible plan? Using an = instead of : when passing the value in isn't fixing the issue.

Upvotes: 1

Views: 6314

Answers (1)

SztupY
SztupY

Reputation: 10546

If you have a JSON based API, then since ansible 2.0 you can use body_format: json parameter on the uri module, and actually supply the body in YAML format, and let ansible convert it to JSON.

Doing this is not only nicer, but you are less prone to substitution issues:

- hosts: localhost
  vars:
    somevar: data
  tasks:
    - name: Create a repo
      uri:
        url: http://mystash.com/stash/rest/api/1.0/projects/PROJECT/repos/
        method: POST
        body_format: json
        body:
          name: "{{ somevar }}_settings"
        force_basic_auth: yes
        status_code: 201
        headers:
          Content-Type: "application/json"
          Authorization: "Basic bm90bXlwYXNzd29yZA==="
          Accept: "application/json"

This will for example send

{"name":"data_settings"}

as the body

Upvotes: 6

Related Questions