# Comparing two datasets

**URL:** <https://forum.ansible.com/t/comparing-two-datasets/19288>\
**Category:** Ansible Project\
**Created:** [September 17, 2015, 1:43pm UTC](https://forum.ansible.com/t/comparing-two-datasets/19288 "2015-09-17T13:43:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [September 17, 2015, 1:43pm UTC](https://forum.ansible.com/t/comparing-two-datasets/19288/1 "2015-09-17T13:43:44Z")

</div>

Hello,

I have a Grafana installation and I would like to query the HTTP API and then compare the data sources on the server to a dict for the data sources. If one of the data sources in the dictionary is new I would like to create it (later I will work on changing).

I’m thing that I need to do some type of nested loop but, can’t seem to figure it out.

Any help would be very much appreciated.

Thanks,  
Hank

This is what I have in the dictionary:

`- debug: var=grafana_ds`

`TASK: [grafana | debug var=grafana_ds] **************************************** `  
`<some_server> ESTABLISH CONNECTION FOR USER: ansible`  
`ok: [some_server] => {`  
`"var": {`  
`"grafana_ds": {`  
`"cache_stats": {`  
`"access": "direct",`  
`"basicAuth": false,`  
`"basicAuthPassword": "password",`  
`"basicAuthUser": "user",`  
`"database": "cache_stats",`  
`"isDefault": false,`  
`"jsonData": null,`  
`"password": "password",`  
`"type": "influxdb",`  
`"url": ["https://some_other_server"](https://some_other_server),`  
`"user": "user"`  
`},`  
`"deliveryservice_stats": {`  
`"access": "direct",`  
`"basicAuth": false,`  
`"basicAuthPassword": "basicpassword",`  
`"basicAuthUser": "basicuser",`  
`"database": "deliveryservice_stats",`  
`"isDefault": false,`  
`"jsonData": null,`  
`"password": "password",`  
`"type": "influxdb",`  
`"url": ["https://some_other_server"](https://some_other_server),`  
`"user": "user"`  
`},`  
`"test_stats": {`  
`"access": "direct",`  
`"basicAuth": false,`  
`"basicAuthPassword": "basicpassword",`  
`"basicAuthUser": "basicuser",`  
`"database": "test_stats",`  
`"isDefault": false,`  
`"jsonData": null,`  
`"password": "password",`  
`"type": "influxdb",`  
`"url": ["https://some_other_server"](https://some_other_server),`  
`"user": "user"`  
`}`  
`}`  
`}`  
`}`

This is what the HTTP API Returns:

`- name: Get a list of Data Sources`  
`uri:`  
`url: [https://](https://){{ inventory_hostname }}/api/datasources`  
`return_content: yes`  
`method: GET`  
`HEADER_Authorization: Bearer {{grafana_api_key}}`  
`HEADER_Accept: application/json`  
`HEADER_Content-Type: application/json`  
`validate_certs: no`  
`register: playbook_grafana_ds`

`TASK: [grafana | debug var=playbook_grafana_ds.json] ************************** `  
`<some_server> ESTABLISH CONNECTION FOR USER: ansible`  
`ok: [some_server] => {`  
`"var": {`  
`"playbook_grafana_ds.json": [`  
`{`  
`"access": "direct",`  
`"basicAuth": false,`  
`"basicAuthPassword": "",`  
`"basicAuthUser": "",`  
`"database": "cache_stats",`  
`"id": 1,`  
`"isDefault": false,`  
`"jsonData": null,`  
`"name": "cache_stats",`  
`"orgId": 1,`  
`"password": "password",`  
`"type": "influxdb",`  
`"url": ["https://some_other_server"](https://some_other_server),`  
`"user": "user"`  
`},`  
`{`  
`"access": "direct",`  
`"basicAuth": false,`  
`"basicAuthPassword": "",`  
`"basicAuthUser": "",`  
`"database": "deliveryservice_stats",`  
`"id": 2,`  
`"isDefault": false,`  
`"jsonData": null,`  
`"name": "deliveryservice_stats",`  
`"orgId": 1,`  
`"password": "password",`  
`"type": "influxdb",`  
`"url": ["https://some_other_server"](https://some_other_server),`  
`"user": "user"`  
`}`  
`]`  
`}`  
`}`

Here is the start of the task that I’m trying to accomplish but, doesn’t even come close to working:

`- name: Create Data Sources`  
`uri:`  
`url: ``[https://](https://){{ inventory_hostname }}/api/datasources`  
`return_content: yes`  
`method: POST`  
`HEADER_Authorization: Bearer {{grafana_api_key}}`  
`HEADER_Accept: application/json`  
`HEADER_Content-Type: application/json`  
`body: "{{ lookup('template','../templates/datasources.json.j2') }}"`  
`validate_certs: no`  
`when: item.key not in playbook_grafana_ds.json`  
`with_dict: grafana_ds`

---

<div class="post-metadata">

**Author:** ![Hank\_Beatty](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@Hank\_Beatty](https://forum.ansible.com/u/Hank_Beatty)\
**Post date:** [September 21, 2015, 2:50pm UTC](https://forum.ansible.com/t/comparing-two-datasets/19288/2 "2015-09-21T14:50:30Z")

</div>

Hello,

Here is the solution a co-worker and I came up with:

playbook:

`# result is a temp var`  
`- name: Get a list of Data Sources`  
`uri:`  
`url: [https://](https://){{ inventory_hostname }}/api/datasources`  
`return_content: yes`  
`method: GET`  
`HEADER_Authorization: Bearer {{grafana_api_key}}`  
`HEADER_Accept: application/json`  
`HEADER_Content-Type: application/json`  
`validate_certs: no`  
`register: result`

`- debug: var=result.json`

`- set_fact: pb_ds="{{ result.json|map(attribute='name')|list }}"`

`- debug: var=pb_ds`

`- debug: var=data_sources`

`- debug: msg="{{item.key}} not there {{item.value['type']}}"`  
`when: item.key not in pb_ds`  
`with_dict: data_sources`

`- name: Create Data Sources`  
`uri:`  
`url: [https://](https://){{ inventory_hostname }}/api/datasources`  
`return_content: yes`  
`method: POST`  
`HEADER_Authorization: Bearer {{grafana_api_key}}`  
`HEADER_Accept: application/json`  
`HEADER_Content-Type: application/json`  
`body: "{{ lookup('template','../templates/datasources.json.j2') }}"`  
`validate_certs: no`  
`when: item.key not in pb_ds`  
`with_dict: data_sources`

The data\_sources dictionary:

`data_sources:`  
`cache_stats:`  
`url: [https://](https://)<some_server>`  
`access: direct`  
`type: influxdb`  
`password: password`  
`user: user`  
`database: cache_stats`  
`basicAuth: 'false'`  
`basicAuthUser: user`  
`basicAuthPassword: password`  
`isDefault: 'false'`  
`jsonData: 'null'`  
`deliveryservice_stats:`  
`url: [https://](https://)<some_server>`  
`access: direct`  
`type: influxdb`  
`password: password`  
`user: user`  
`database: deliveryservice_stats`  
`basicAuth: 'false'`  
`basicAuthUser: basicuser`  
`basicAuthPassword: basicpassword`  
`isDefault: 'false'`  
`jsonData: 'null'`
