Kevin
Kevin

Reputation: 143

Ansible get value from array

From the following json resonse I want to get all the "displayName" values where in the "tags" no key containing "Support Team" is present.

In this case I would expect "Apache Web Server" as an answer.

"msg": "JMESPathError in json_query filter plugin:\nIn function contains() expected one of: ['array', 'string'], received: "object"

[
  {
    "entityId": "PROCESS_GROUP_INSTANCE-16CA2E1741B16EF7",
    "displayName": "Apache Web Server ",
    "discoveredName": "Apache Web Server ",
    "firstSeenTimestamp": 1624209179999,
    "lastSeenTimestamp": 1634576939999,
    "tags": [
      {
        "context": "CONTEXTLESS",
        "key": "Apache - HTTP Server",
        "value": "Apache Web Server "
      },
      {
        "context": "CONTEXTLESS",
        "key": "Description",
        "value": "vcc_genesys_mcp_b_[prd]"
      },
      {
        "context": "CONTEXTLESS",
        "key": "Host",
        "value": "xx.xx.xx.x,"
      }
    ],
    "fromRelationships": {
      "isProcessOf": [
        "HOST-C2B40BEA8D1EDEAF"
      ],
      "isInstanceOf": [
        "PROCESS_GROUP-D47E25FDCCAFB058"
      ]
    },
    "toRelationships": {},
    "metadata": {
      "commandLineArgs": [
        "/usr/sbin/httpd -V"
      ],
      "executables": [
        "httpd"
      ],
      "executablePaths": [
        "/usr/sbin/httpd"
      ]
    },
    "softwareTechnologies": [
      {
        "type": "APACHE_HTTPD",
        "edition": null,
        "version": "2.4.6"
      }
    ],
    "bitness": "64bit",
    "monitoringState": {
      "actualMonitoringState": "OFF",
      "expectedMonitoringState": "ON",
      "restartRequired": false
    }
  },
  {
    "entityId": "PROCESS_GROUP_INSTANCE-56E5C00FAE04F9F3",
    "displayName": "Apache Web Server WebOfficeApache",
    "discoveredName": "Apache Web Server WebOfficeApache",
    "firstSeenTimestamp": 1619099280000,
    "lastSeenTimestamp": 1634709180000,
    "tags": [
      {
        "context": "CONTEXTLESS",
        "key": "Apache - HTTP Server",
        "value": "Apache Web Server WebOfficeApache"
      },
      {
        "context": "CONTEXTLESS",
        "key": "Application",
        "value": "VCC - Genesys - GWT"
      },
      {
        "context": "CONTEXTLESS",
        "key": "Description",
        "value": "vcc_genesys_gwt_d_[prd]"
      },
      {
        "context": "CONTEXTLESS",
        "key": "Host",
        "value": "xx.xx.xx.xx,"
      },
      {
        "context": "CONTEXTLESS",
        "key": "Support Team - TIO Contact Center Support"
      }
    ],
    "fromRelationships": {
      "isProcessOf": [
        "HOST-751FB31157FE2887"
      ],
      "isInstanceOf": [
        "PROCESS_GROUP-23FA731EF9FAD174"
      ]
     
    },
    "metadata": {
      "commandLineArgs": [
        "httpd -k start -f /opt/genesys/gcti/WebOfficeApache/httpd.conf -DFOREGROUND"
      ],
    },
    "softwareTechnologies": [
      {
        "type": "APACHE_HTTPD",
        "edition": null,
        "version": "2.4.6"
      }
    ],
    "listenPorts": [
      8080
    ],
    "bitness": "64bit",
    "modules": [
      "mod_auth_basic.c",
    ],
    "monitoringState": {
      "actualMonitoringState": "ON",
      "expectedMonitoringState": "ON",
      "restartRequired": true
    },
    "agentVersions": [
      {
        "major": 1,
        "minor": 221,
        "revision": 131,
        "timestamp": "20210727-144835",
        "sourceRevision": ""
      }
    ],
    "managementZones": [
      {
        "id": "3595395043409676188",
        "name": "VCC - Genesys - GWT"
      },
      {
        "id": "7589108031829358004",
        "name": "VCC"
      }
    ]
  },
...
]

Playbook:

- name: List all configured auto-tags
      uri:
          url: "https://xx.xx-xx.xx{{ prd_env }}api/v1/entity/infrastructure/process-groups?includeDetails=true"          
          method: GET
          headers:
            Content-Type: application/json; charset=utf-8
            Authorization: "{{ prd_auth }}"
          return_content: yes  
      register: data_tags_prd
                
    - name: Print all configured auto-tags 
      debug:
        var: data_tags_prd.json
       
    - debug:
        msg: "{{ data_tags_prd.json | json_query(query) }}"
      vars:
        query: "[?!contains(@, 'Support team').displayName]"

Upvotes: 1

Views: 1283

Answers (1)

Frenchy
Frenchy

Reputation: 17007

when the condition becomes complex on complex structures i prefer to use a custom filter:

you create a folder filter_plugins in your playbook folder (i have named the file myfilters.py and the filter customfilter)

#!/usr/bin/python
class FilterModule(object):
    def filters(self):
        return {   
            'customfilter': self.customfilter
        }

    def customfilter(self, obj, exclude = ''):
        result = []
        for r in obj:
            strings = [x['key'] for x in r['tags']] 
            if not any(exclude in string for string in strings):
                result.append(r["displayName"].strip())
        #print(result) 
        return result

the filter is little generic, you could notify the string to exclude:

- name: vartest
  hosts: localhost
  vars:
    output: "{{ lookup('file', 'file.json') }}"  # just for test

  tasks:

    - name: display output names
      debug:
        msg: "{{ output | customfilter('Support Team')}}"

result:

ok: [localhost] => {
    "msg": [
        "Apache Web Server"
    ]
}

the result will be a list of displaynames with the condition wanted

Upvotes: 1

Related Questions