# uri\_module POST with template as body, is it possible?

**URL:** <https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330>\
**Category:** Ansible Project\
**Created:** [September 22, 2015, 6:38pm UTC](https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330 "2015-09-22T18:38:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Par\_Svensson](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Par\_Svensson](https://forum.ansible.com/u/Par_Svensson)\
**Post date:** [September 22, 2015, 6:38pm UTC](https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330/1 "2015-09-22T18:38:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [September 22, 2015, 8:20pm UTC](https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330/2 "2015-09-22T20:20:38Z")

</div>

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

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

-Toshio

---

<div class="post-metadata">

**Author:** ![Par\_Svensson](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@Par\_Svensson](https://forum.ansible.com/u/Par_Svensson)\
**Post date:** [September 22, 2015, 8:58pm UTC](https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330/3 "2015-09-22T20:58:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Senthilkumar\_Ganesan](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Senthilkumar\_Ganesan](https://forum.ansible.com/u/Senthilkumar_Ganesan)\
**Post date:** [November 3, 2016, 7:47am UTC](https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330/4 "2016-11-03T07:47:38Z")

</div>

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

Thanks!  
Senthil

---

<div class="post-metadata">

**Author:** ![Aleksandar\_Kasabov](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@Aleksandar\_Kasabov](https://forum.ansible.com/u/Aleksandar_Kasabov)\
**Post date:** [January 10, 2017, 11:44am UTC](https://forum.ansible.com/t/uri-module-post-with-template-as-body-is-it-possible/19330/5 "2017-01-10T11:44:12Z")

</div>

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

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

```
