'Ansible json query
I have the following json:
{
"entities": {
"results": [
{
"content": "",
"redirected": false,
"url": "",
"status": 200,
"date": "Fri, 18 Mar 2022 11:55:46 GMT",
"content_type": "application/json",
"content_length": "6958",
"connection": "close",
"set_cookie": "m",
"x_oneagent_js_injection": "true",
"response_source": "Cluster",
"response_id": "TD4QHMLVDNGP",
"strict_transport_security": "preload",
"x_xss_protection": "1; mode=block",
"cache_control": "no-store, no-cache",
"pragma": "no-cache",
"x_robots_tag": "noindex",
"vary": "Accept-Encoding, User-Agent",
"cookies_string": "c8c",
"cookies": {
"dtCookie": "",
"apmroute": ""
},
"msg": "OK (6958 bytes)",
"elapsed": 0,
"changed": false,
"json": {
"totalCount": 1,
"pageSize": 50,
"entities": [
{
"entityId": "xxxx",
"displayName": "xxxx",
"toRelationships": {
"isNetworkClientOfHost": [
],
"isProcessOf": [
],
"isNetworkInterfaceOf": [
],
"runsOn": [
],
"runsOnHost": [
{
"id": "SERVICE-917FBB145BB92C69",
"type": "SERVICE"
},
{
"id": "SERVICE_INSTANCE-C278DFBEF7827B65",
"type": "SERVICE_INSTANCE"
},
{
"id": "SERVICE-9EDC19D785C5FE9A",
"type": "SERVICE"
},
{
"id": "SERVICE_INSTANCE-517C145645D012A8",
"type": "SERVICE_INSTANCE"
},
{
"id": "SERVICE-F39E62B8805CD48D",
"type": "SERVICE"
},
{
"id": "SERVICE_INSTANCE-C3193EF273B614BE",
"type": "SERVICE_INSTANCE"
},
{
"id": "SERVICE_INSTANCE-94DF9711806A0297",
"type": "SERVICE_INSTANCE"
}
],
"isDiskOf": [
]
}
}
]
},
"invocation": {
"module_args": {
"url": "",
"method": "GET",
"headers": {
"Content-Type": "application/json; charset=utf-8",
"Authorization": ""
},
"return_content": true,
"status_code": [
"200"
],
"validate_certs": false,
"force": false,
"http_agent": "ansible-httpget",
"use_proxy": true,
"force_basic_auth": false,
"body_format": "raw",
"follow_redirects": "safe",
"timeout": 30,
"follow": false,
"unsafe_writes": false,
"url_username": null,
"url_password": null,
"client_cert": null,
"client_key": null,
"dest": null,
"body": null,
"src": null,
"creates": null,
"removes": null,
"unix_socket": null,
"mode": null,
"owner": null,
"group": null,
"seuser": null,
"serole": null,
"selevel": null,
"setype": null,
"attributes": null,
"content": null,
"backup": null,
"remote_src": null,
"regexp": null,
"delimiter": null,
"directory_mode": null
}
},
"failed": false,
"item": "exxx",
"ansible_loop_var": "item"
},
{
"content": "",
"redirected": false,
"url": "",
"status": 200,
"date": "Fri, 18 Mar 2022 11:55:46 GMT",
"content_type": "application/json",
"transfer_encoding": "chunked",
"connection": "close",
"set_cookie": "",
"dynatrace_response_source": "Cluster",
"dynatrace_response_id": "",
"strict_transport_security": "",
"x_xss_protection": "1; mode=block",
"cache_control": "no-store, no-cache",
"pragma": "no-cache",
"vary": "Accept-Encoding, User-Agent",
"x_robots_tag": "noindex",
"cookies_string": "",
"cookies": {
"dtCookie": "-",
"apmroute": ""
},
"msg": "OK (unknown bytes)",
"elapsed": 0,
"changed": false,
"json": {
"totalCount": 1,
"pageSize": 50,
"entities": [
{
"entityId": "xxxx",
"displayName": "xxxx",
"toRelationships": {
"isProcessOf": [
],
"isNetworkClientOfHost": [
],
"isDiskOf": [
],
"runsOn": [
],
"isNetworkInterfaceOf": [
]
}
}
]
},
"invocation": {
"module_args": {
"url": "9&=",
"method": "GET",
"headers": {
"Content-Type": "/json; =utf-8",
"Authorization": ""
},
"return_content": true,
"status_code": [
"200"
],
"validate_certs": false,
"force": false,
"http_agent": "ansible-httpget",
"use_proxy": true,
"force_basic_auth": false,
"body_format": "raw",
"follow_redirects": "safe",
"timeout": 30,
"follow": false,
"unsafe_writes": false,
"url_username": null,
"url_password": null,
"client_cert": null,
"client_key": null,
"dest": null,
"body": null,
"src": null,
"creates": null,
"removes": null,
"unix_socket": null,
"mode": null,
"owner": null,
"group": null,
"seuser": null,
"serole": null,
"selevel": null,
"setype": null,
"attributes": null,
"content": null,
"backup": null,
"remote_src": null,
"regexp": null,
"delimiter": null,
"directory_mode": null
}
},
"failed": false,
"item": "xx",
"ansible_loop_var": "item"
}
],
"msg": "All items completed",
"changed": false
},
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false
}
I want to extract the id's with "type": "SERVICE". When i use the below query i get the result but also including the type: SERVICE_INSTANCE
- debug:
msg: "{{ entities | json_query('results[].json.entities[].toRelationships.runsOnHost[].id') }}"
I tried with the below query as documented on https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html
But when i try this the set fact stays empty. I tried with different kind of quots, single, double,.. but it stays empty.
- set_fact:
my_service: "{{ entities | json_query('results[].json.entities[].toRelationships.runsOnHost[?type==''SERVICE''].id') }}"
Any suggestions?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
