# Query Regarding REST API integration in Ansible

**URL:** <https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282>\
**Category:** Ansible Project\
**Created:** [September 16, 2019, 7:30am UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282 "2019-09-16T07:30:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [September 16, 2019, 7:30am UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/1 "2019-09-16T07:30:31Z")

</div>

Hi Ansible Gurus, specially Dick Visser and vlado and all experts,  
I have a question regarding Ansible Integration of REST APIs (built in JAVA). Assume I have 100 of Resource End Point( or Service End point) where each Resource can be updated/read/deleted using CURD operation. Also Each resource has almost 10-15 variables (JSON Req/Resp).  
Currently i am using URI module for interacting these webservices but my whole JSON data is stored in raw text file. Any time i want to do any CURD opeation , i update that file and call Ansible task to perform respective CURD operation.

Business demand is : have each REST resource parameters also configurable using ansible (rather than storing in Plain text file). What is the best way of doing this because I have many REST resources and each resource has many params.(as req./resp).

Waiting for urgent response and best solution.

Thanks and Regards  
Rahul

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 16, 2019, 9:53am UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/2 "2019-09-16T09:53:57Z")

</div>

Hi

What do your tasks look like?  
In any case, the uri module accept an ansible native format (i.e. YAML  
list) for the body parameters:  
[https://docs.ansible.com/ansible/latest/modules/uri\_module.html](https://docs.ansible.com/ansible/latest/modules/uri_module.html).  
So just use that instead of an external JSON file.

BTW, keywords like "urgent" tend to not work very well in community  
driven support fora.

Dick

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [September 16, 2019, 12:28pm UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/3 "2019-09-16T12:28:50Z")

</div>

Thanks Dick! Below is the task:

- name: application provisionning  
uri:  
url: http://{{ IP | default(ansible\_fqdn) }}:8080/my-config/rest  
body\_format: json  
method: PUT  
body: “{{ lookup(‘file’,app\_provisioning\_filepath) | from\_json}}”  
user: “{{ restapi\_user }}”  
password: “{{ restapi\_password }}”  
force\_basic\_auth: true

Actually problem is : This Data (Rest Response/Req.) is quite big(present in file) and we want to make each key(variable) of this data in Ansible either using jinja template or anything else.

Rahul

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [November 5, 2019, 10:26am UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/4 "2019-11-05T10:26:19Z")

</div>

Hi Dick ,  
Any suggestion on above query?

Rahul

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [November 12, 2019, 10:41am UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/5 "2019-11-12T10:41:16Z")

</div>

Hi dick and all,  
Any suggestion on this ??

@ Dick

NO ,the uri module does not accept an ansible native format (i.e. YAML  
list) for the body parameters:

---

<div class="post-metadata">

**Author:** ![Sumit\_Jaiswal](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Sumit\_Jaiswal](https://forum.ansible.com/u/Sumit_Jaiswal)\
**Post date:** [November 12, 2019, 12:42pm UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/6 "2019-11-12T12:42:07Z")

</div>

Hi Rahul,

From your query what I understood was, you’ve have REST API that has `n` param and you want to handle each API and it’s param as single resource. And if that’s correct, then in that case you can write a module using HTTPAPI as connection plugin handle each of APIs and its resource in particular module.

Please refer: [https://docs.ansible.com/ansible/latest/plugins/httpapi.html](https://docs.ansible.com/ansible/latest/plugins/httpapi.html) and [https://docs.ansible.com/ansible/latest/plugins/connection/httpapi.html](https://docs.ansible.com/ansible/latest/plugins/connection/httpapi.html), this will guide you through the use of HTTPAPI plugin and platforms which have already implemented their REST APIs using the plugin.

**_Regards,_**  
Sumit Jaiswal  
Github/IRC: _justjais_

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [November 12, 2019, 1:06pm UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/7 "2019-11-12T13:06:37Z")

</div>

Hi Sumit,  
My requirement is like below:  
I have multiple REST end points (Resource END points ) and each one of these has multiple params .  
Example:  
~/base/endpoint1 - has n params  
~/base/endpoint2 - has m params  
~/base/endpoint3 - has p params  
~/base/endpoint4 - has q params

… and so on .

All of these  
On each of these resource , one can perform CURD operations.  
Notice implementation of this code is in JAVA.

So once i automate using Ansible , I use Ansible URI module and POST all data using a .json file ( which has all cumulative data of all resources i.e endpoint1+endpoint2+…)as below:  
name: application provisionning  
uri:  
url: http://{{ IP | default(ansible\_fqdn) }}:8080/myapp/base  
body\_format: json  
method: POST  
body: “{{ lookup(‘file’,app\_provisioning\_filepath) | from\_json}}”  
user: “{{ restapi\_user }}”  
password: “{{ restapi\_password }}”  
force\_basic\_auth: true

Now my requirment is :  
End user wants that each of these parameters whatever u store in (.json file) should be ansible format (e.g .yaml format).  
So that whole ansible code looks in .yaml and no .json etc.  
Now our REST API can produce/consume only JSON media type as per implementation.

How i can make all these params( which is stored in .json file) into ansible or yaml format.

---

<div class="post-metadata">

**Author:** ![Sumit\_Jaiswal](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Sumit\_Jaiswal](https://forum.ansible.com/u/Sumit_Jaiswal)\
**Post date:** [November 12, 2019, 1:43pm UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/8 "2019-11-12T13:43:17Z")

</div>

Hi Rahul,

If you just want to convert .json data to .yaml data then you can use the `to_yaml` filter, ref: [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#filters-for-formatting-data](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data)

**_Regards,_**  
Sumit Jaiswal  
Github/IRC: _justjais_

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [November 12, 2019, 2:12pm UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/9 "2019-11-12T14:12:01Z")

</div>

THanks Sumit ,  
So I can convert whole .yaml file( Which contains all REST data) to .json file (which will contain json data).  
Can you give snippet of code for yaml file to json file conversion

Regards  
Rahul

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 12, 2019, 2:21pm UTC](https://forum.ansible.com/t/query-regarding-rest-api-integration-in-ansible/31282/10 "2019-11-12T14:21:41Z")

</div>

"{{lookup('file', 'yaml.yml') |to\_json }}"
