我需要从ansible播放中获取json中的key值。问题是-不确定密钥是否在特定的Json级别。可能因json而异。因此,我需要盲目地“搜索” json中的密钥并获取它们的值[不确定可用的密钥有多少,可用的深度有多大]。在这里,我给出示例json并播放以实现相同效果。
JSON数据:
{
"status": "SUCCESS",
"errors": [
{
"jobname": "DbcAllJob_td_part_20190211150403_1",
"errorList": [
{
"id": "1",
"migrationId": "b9e19f6b-bc2e-4e2b-8fe9-20efb74d8289",
"jobId": "27",
"backupPhase": "DATA",
"restorePhase": "NULL",
"errorType": "ABORT",
"atTimeStamp": "2019/02/11 15:27:46",
"errorStatus": "UNRESOLVED",
"errorcode": "1148",
"errortext": "1148: DSA DSMAIN-BARNC Communication Exception: 1001:(**********K) recv() call has EOF",
"report": {
"root": {
"system": "TARGET",
"host": "**********",
"state_text": "2019/02/11 07:26:52.181 [ 27:29152:29152] ERROR Executor - Exception received by master thread: recv() call has EOF",
"entityId": "**********_clienthandler",
"logTime": "2019/02/11 07:26:52.181",
"peers": [
"**********_clienthandler"
],
"phase": "",
"rootError": "BarNC - User shutdown request from command line received, stopping BARNC",
"software": "clienthandler"
},
"sequence": [
{
"host": "sdt09586",
"state_text": "Feb 11 07:25:20 sdt09586 DSA[27578]: CRITICAL: DSA: 4751002 #CRITICAL: DataStreamController: 4751002 #DSC version[16.20.24.00], Commons version[16.20.24.00], DSC cannot connect to activeMQ Broker.",
"entityId": "sdt09586_messages",
"logTime": "2019/02/11 7:25:20.000",
"peers": [],
"phase": "",
"software": "messages"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "Feb 11 07:26:52 ********** Teradata[19593]: INFO: Teradata: 6670 # DSMAIN 1148: DSA DSMAIN-BARNC Communication Exception: 1001:(**********K) recv() call has EOF",
"entityId": "**********_messages",
"logTime": "2019/02/11 7:26:52.000",
"peers": [],
"phase": "",
"software": "messages"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "Feb 11 07:26:52 ********** Teradata[6369]: INFO: Teradata: 3265 #Transaction has been Aborted by Administrator or Operations Staff.",
"entityId": "**********_messages",
"logTime": "2019/02/11 7:26:52.000",
"peers": [],
"phase": "",
"software": "messages"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "2019/02/11 07:26:52.900 (134226672): Exception rethrown caught in DSMBackupTask::BeginWriteTask.",
"entityId": "27_**********_barlog_26623_34_3_3",
"logTime": "2019/02/11 07:26:52.900",
"peers": [
"AMP =1"
],
"phase": "",
"software": "barlog_26623_34_3_3"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "2019/02/11 07:26:52.900 (134226672): SendInternalAbort assumeMsgFromStpAab = 1",
"entityId": "27_**********_barlog_26623_34_3_3",
"logTime": "2019/02/11 07:26:52.900",
"peers": [
"AMP =1"
],
"phase": "",
"software": "barlog_26623_34_3_3"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "2019/02/11 07:26:52.900 (134226672): SendInternalAbort, partition = 34, jobId = 27",
"entityId": "27_**********_barlog_26623_34_3_3",
"logTime": "2019/02/11 07:26:52.900",
"peers": [
"AMP =1"
],
"phase": "",
"software": "barlog_26623_34_3_3"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "2019/02/11 07:26:52.900 (134226672): errorCode = 1148, errorTextLength = 84, errorText = 1148: DSA DSMAIN-BARNC Communication Exception: 1001:(**********K) recv() call has EOF",
"entityId": "27_**********_barlog_26623_34_3_3",
"logTime": "2019/02/11 07:26:52.900",
"peers": [
"AMP =1"
],
"phase": "",
"software": "barlog_26623_34_3_3"
},
{
"system": "SOURCE",
"host": "**********",
"state_text": "2019/02/11 07:26:52.910 (134254672): AbortTask has a message...",
"entityId": "27_**********_barlog_26623_34",
"logTime": "2019/02/11 07:26:52.910",
"peers": [],
"phase": "DATA/BUILD",
"software": "barlog_26623_34"
}
]
},
"remedy": [
{
"seqNum": 1,
"remedy": "Configure all machines to have same bynet configuration",
"remedyOpcode": "RECONFIGURE_BYNET",
"remedyInfo": [
{
"systemName": "**********",
"ipAddress": "**********",
"username": "**********",
"password": "**********",
"osUser": "root"
},
{
"systemName": "**********",
"ipAddress": "**********",
"username": "**********",
"password": "**********",
"osUser": "root"
}
]
},
{
"seqNum": 2,
"remedy": "Bring ClientHandler up on **********",
"remedyOpcode": "RESTART_SERVICE",
"remedyInfo": {
"systemName": "**********",
"ipAddress": "**********",
"username": "**********",
"password": "**********",
"osUser": "root"
}
},
{
"seqNum": 3,
"remedy": "Re-submit the job",
"remedyOpcode": "RESUBMIT",
"remedyInfo": null
}
],
"detailedreport": [
{
"component": "TPANode : **********",
"description": "TPANODE status : Up and Running",
"additionalProperties": {}
},
{
"component": "TPANode : **********",
"description": "TPANODE status : Up and Running",
"additionalProperties": {}
},
{
"component": "Bynet : [**********, **********]",
"description": "Bynet configuration mismatch",
"additionalProperties": {}
},
{
"component": "ClientHandler : **********",
"description": "ClientHandler status : not running",
"additionalProperties": {}
},
{
"component": "ActiveMQ : **********",
"description": "ActiveMQ status : running",
"additionalProperties": {}
},
{
"component": "ActiveMQ Queue: **********",
"description": "ActiveMQ message count : 13",
"additionalProperties": {}
},
{
"component": "DSC : **********",
"description": "DSC status : running, DSARest status : running",
"additionalProperties": {}
},
{
"component": "DSC Processes: **********",
"description": "Number of DSC processes running : 1",
"additionalProperties": {}
},
{
"component": "DSMAIN : **********",
"description": "DSMain status : Up and Running, Left over Processes : No left over processes",
"additionalProperties": {}
},
{
"component": "DSMAIN : **********",
"description": "DSMain status : Up and Running, Left over Processes : No left over processes",
"additionalProperties": {}
},
{
"component": "DatabaseConnection : **********",
"description": "Database status : UP",
"additionalProperties": {}
},
{
"component": "DatabaseConnection : **********",
"description": "Database status : UP",
"additionalProperties": {}
},
{
"component": "DatabaseConnection : **********",
"description": "Database status : UP",
"additionalProperties": {}
}
]
},
{
"id": "2",
"migrationId": "b9e19f6b-bc2e-4e2b-8fe9-20efb74d8289",
"jobId": "27",
"backupPhase": null,
"restorePhase": null,
"errorType": "ABORT",
"atTimeStamp": "",
"errorStatus": null,
"errorcode": "1160",
"errortext": "An error occurred restoring statistics.",
"report": {
"root": null,
"sequence": null
},
"remedy": [
{
"seqNum": -1,
"remedy": null,
"remedyOpcode": null,
"remedyInfo": null
}
],
"detailedreport": [
{
"component": null,
"description": null,
"additionalProperties": {}
}
]
}
]
}
]
}
从json上方,我需要读取“补救”值。但不确定它们始终处于什么级别。
这是相同的ansible脚本。
---
- hosts: localhost
tasks:
- name: loop
set_fact:
migration_detail_status: "{{ lookup('file', 'inj_error.json') }}"
until: "'{{ migration_detail_status.errors.0.errorList.0.remedy.1.remedy | search('Bring ClientHandler up on') }}' and '{{ migration_detail_status.errors.0.errorList.0.remedy.1.remedyOpcode | search('RESTART_SERVICE') }}'"
retries: 2
delay: 5
ignore_errors: yes
- debug:
var: migration_detail_status.errors.0.errorList.0.remedy.1.remedyOpcode
在until
条件下,我遍历了json,因为我知道json的格式。但是,并非所有情况都一样。因此,请帮助遍历以从json获得搜索key:value
,而不论其级别如何。
答案 0 :(得分:0)
您可以使用JMESPath查询remedy
获取所有errors[].errorList[].remedy[].remedy
的列表。
[
"Configure all machines to have same bynet configuration",
"Bring ClientHandler up on **********",
"Re-submit the job"
]
在Ansible中,您将使用json_query
过滤器来应用JMESPath查询。
如果您还需要操作码:errors[].errorList[].remedy[].{r:remedy,o:remedyOpcode}
[
{
"r": "Configure all machines to have same bynet configuration",
"o": "RECONFIGURE_BYNET"
},
{
"r": "Bring ClientHandler up on **********",
"o": "RESTART_SERVICE"
},
{
"r": "Re-submit the job",
"o": "RESUBMIT"
},
{
"r": null,
"o": null
}
]
示例:
- debug:
msg: "{{ migration_detail_status | json_query('errors[].errorList[].remedy[].remedy') }}"