Comparing two datasets

34 views
Skip to first unread message

Hank Beatty

unread,
Sep 17, 2015, 9:43:50 AM9/17/15
to ansible...@googlegroups.com
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",
                "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",
                "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",
                "user": "user"
            }
        }
    }
}

This is what the HTTP API Returns:

- name: Get a list of Data Sources
  uri:
    url: 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",
                "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",
                "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://{{ 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

Hank Beatty

unread,
Sep 21, 2015, 10:50:42 AM9/21/15
to ansible...@googlegroups.com
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://{{ 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://{{ 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://<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://<some_server>
    access: direct
    type: influxdb
    password: password
    user: user
    database: deliveryservice_stats
    basicAuth: 'false'
    basicAuthUser: basicuser
    basicAuthPassword: basicpassword
    isDefault: 'false'
    jsonData: 'null'
Reply all
Reply to author
Forward
0 new messages