uri_module POST with template as body, is it possible?

6,384 views
Skip to first unread message

Pär Svensson

unread,
Sep 22, 2015, 2:48:21 PM9/22/15
to Ansible Project
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?

Toshio Kuratomi

unread,
Sep 22, 2015, 4:20:57 PM9/22/15
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/43016a1f-b11b-4e7a-bc11-c6358185f677%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Pär Svensson

unread,
Sep 22, 2015, 4:58:50 PM9/22/15
to Ansible Project
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

Senthilkumar Ganesan

unread,
Nov 3, 2016, 10:57:12 AM11/3/16
to Ansible Project
Even I've tried with same approach, but getting an error. Can you show the content of datasource.json.j2 file

Thanks!
Senthil

Aleksandar Kasabov

unread,
Jan 10, 2017, 12:21:03 PM1/10/17
to Ansible Project
One could even skip the roles/grafana/templates/ part in the lookup:

body: "{{lookup('template', 'http/updateRepository.txt')}}"
Reply all
Reply to author
Forward
0 new messages