0

我需要从 ansible play 中获取 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。但是,并非所有情况都相同。因此,请帮助遍历以key:value从 json 进行搜索,无论其级别如何。

4

1 回答 1

0

remedy您可以使用 JMESPath 查询获得所有 's 的列表: 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') }}"
于 2019-02-22T05:59:24.277 回答