Query Regarding REST API integration in Ansible

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

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.
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

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

Hi Dick ,
Any suggestion on above query?

Rahul

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:

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 and 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

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.

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

Regards,
Sumit Jaiswal
Github/IRC: justjais

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

"{{lookup('file', 'yaml.yml') |to_json }}"