从下面的json响应中我想获得所有的"displayName"值在"标签"没有包含"支持团队"的关键字;是礼物。
在这种情况下,我期望"Apache Web Server"作为答案。
"msg": "JMESPathError在json_query过滤器插件:nIn函数包含()期望:['array', 'string']之一,收到:"对象">
[
{
"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"
}
]
},
...
]
剧本:
- 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]"
当复杂结构的条件变得复杂时,我更喜欢使用自定义过滤器:
创建文件夹filter_plugins在playbook文件夹中(我将文件命名为myfilters.py)和过滤器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
过滤器有点泛型,你可以通知字符串排除:
- name: vartest
hosts: localhost
vars:
output: "{{ lookup('file', 'file.json') }}" # just for test
tasks:
- name: display output names
debug:
msg: "{{ output | customfilter('Support Team')}}"
结果:
ok: [localhost] => {
"msg": [
"Apache Web Server"
]
}
结果将是一个包含所需条件的displayname列表