uri_module POST with template as body, is it possible?

Hello!

I’m trying to make a task that uses a REST api to do some final configuration of a previously installed component.
I want to make a POST request with a body that is a template that uses host specific variables.

The closest i’ve come up with is the following:

  • name: Adding datasource via http api
    uri:
    url: http://{{ inventory_hostname }}:3000/api/datasources
    method: POST
    body_format: json

HEADER_Content-Type: “application/json”
body: “{{ lookup(‘file’ , ‘roles/grafana/templates/datasource.json.j2’) }}”

which sends the template-file as body, but unfortunately without any variables substituted…

Any idea how i should approach the problem?

I believe that using the template lookup instead of the file lookup is
what you want. So something like:

   body: "{{ lookup('template',
'roles/grafana/templates/datasource.json.js') }}"

-Toshio

Thanks for the help Toshio, i have verified that using the template lookup does exactly what I want!

To help others, also note that the path to the template is relative to the location of the task file that invoked it.

/Pär

Even I’ve tried with same approach, but getting an error. Can you show the content of datasource.json.j2 file

Thanks!
Senthil

One could even skip the roles/grafana/templates/ part in the lookup:

body: "{{lookup('template', 'http/updateRepository.txt')}}"