Home > Enterprise >  JSON query in Ansible Playbook failing to select desired data
JSON query in Ansible Playbook failing to select desired data

Time:06-01

I am new to writing Ansible playbooks and have hit a roadblock. I am trying to use the Site24x7 API to schedule maintenance and need to get a specific ID from a list of Monitor Groups. I created the following:

- name: Download Monitor Groups from Site24x7
  uri:
    url: https://www.site24x7.com/api/monitor_groups
    method: GET
    return_content: yes
    headers:
        Accept: "application/json; version=2.1"
        Authorization: "Zoho-oauthtoken {{authtoken.json.access_token}}"
    body: 
    return_content: yes
  register: monitor_groups

- name: Get Monitor Group ID
  set_fact:
    monitorGroupID_query: "[?display_name=='{{hostname.stdout}}'].group_id"

- name: Assign Monitor Group ID
  set_fact:
    monitorGroupID: "{{ monitor_groups.json | json_query(monitor_group_id_query) }}"

- debug:
    var: monitorGroupID

My API call returns data that looks like the following

"monitor_groups.json": {
    "code": 0, 
    "data": [
        {
            "description": "System Generated", 
            "display_name": "server1", 
            "group_id": "319283000000505864", 
            "group_type": 1, 
            "health_threshold_count": 1, 
            "monitors": [
                "319283000000483017"
            ]
        }, 
        {
            "display_name": "server2", 
            "group_id": "319283000004701003", 
            "group_type": 3, 
            "health_threshold_count": 1, 
            "monitors": [
                "319283000003989345", 
                "319283000004061005"
            ]
        }
    ], 
    "message": "success"
}

My query constantly returns an empty string.

TASK [Assign Monitor Group ID] ***************************************************************************************** ok: [server1.fdu.edu] => {"ansible_facts": {"monitorGroupID": ""}, "changed": false}

TASK [debug] ***************************************************************************************** ok: [server1.fdu.edu] => { "monitorGroupID": "" }

Thank you in advance for your help

CodePudding user response:

Single quotes don't work in JMESPath the way it does in almost any other "query language" -- you'll want ` characters wrapped around the JSON literal values. Also, the data:[] in your response is not implied, so if you meant to use it you'll need to either .json.data | json_query or alter your JMESPath to add the data[?display... part

  - name: Assign Monitor Group ID
    set_fact:
      monitorGroupID: "{{ monitor_groups.json | json_query( monitorGroupID_query ) }}"
    vars:
      monitorGroupID_query: 'data[?display_name==`"{{hostname.stdout}}"`].group_id'

given hostname.stdout=server2 yields

ok: [localhost] => {"ansible_facts": {"monitorGroupID": ["319283000004701003"]}, "changed": false}

CodePudding user response:

For example, given the data below

    monitor_groups:
      json:
        code: 0
        data:
          - description: System Generated
            display_name: server1
            group_id: '319283000000505864'
            group_type: 1
            health_threshold_count: 1
            monitors:
              - '319283000000483017'
          - display_name: server2
            group_id: '319283000004701003'
            group_type: 3
            health_threshold_count: 1
            monitors:
              - '319283000003989345'
              - '319283000004061005'
        message: success

Create a dictionary of the hostnames and their group_id. Use it to evaluate the variable monitorGroupID

    name_id: "{{ monitor_groups.json.data|
                 items2dict(key_name='display_name', value_name='group_id') }}"
    monitorGroupID: "{{ name_id[hostname.stdout] }}"

gives

  name_id:
    server1: '319283000000505864'
    server2: '319283000004701003'

Then, the task below

    - debug:
        var: monitorGroupID

gives

TASK [debug] **********************************************************
ok: [server1] => 
  monitorGroupID: '319283000000505864'
ok: [server2] => 
  monitorGroupID: '319283000004701003'
  • Related