在json中进行递归解析并使用ansible获取值

问题描述 投票:0回答:1

我需要从ansible play中获取json中key的值。问题是 - 不确定密钥是否在Json的特定级别。它可能从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,无论他们的级别如何。

json ansible until-loop
1个回答
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') }}"
© www.soinside.com 2019 - 2024. All rights reserved.