首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ansible将CLI命令结果转换为json

使用Ansible将CLI命令结果转换为json
EN

Stack Overflow用户
提问于 2022-03-03 15:54:03
回答 1查看 378关注 0票数 1

下午好伙计们

我需要从5个开关获得BGP表的详细信息。为了做到这一点,我使用Ansible和一个Cisco模块在这5个交换机上发送CLI命令"show bgp l2vpn evpn vrf all“。

它运行良好,但我需要让JSON能够过滤结果。

我尝试的第一个选项是在Ansible中使用\ json选项:

代码语言:javascript
复制
---
- name: Playbook
  hosts: all
  gather_facts: false
  tasks:
  - name: run show bgp on remote devices
    cisco.nxos.nxos_command:
      commands: show bgp l2vpn evpn vrf all | json
    register: cli_result
  - local_action: 
      module: copy 
      content: "{{ cli_result | to_nice_json(indent=2) }}"
      dest: home/cli_result.json

这个选项适用于一个开关,但是一旦我在5个开关上启动它,我就像内存不足或者其他什么东西一样,我得到了错误的错误!一名工人在死州被发现。

我不知道这个问题是来自我正在处理的服务器,这不够强大,或者是其他原因。

另外,我注意到\ json选项会使剧本变得非常慢,当我在没有此选项的情况下启动剧本时,它会快5倍。

所以我的问题是:是否有一种方法可以用将CLI命令结果转换为json?

当然,我尝试使用python模块json来转换它,但是它正在将它转换成一个包含12000行的大块,只有一个键和一个巨大的值。而Ansible中的\ json选项非常清楚地使用了许多不同的键。

以下是CLI命令的结果:

代码语言:javascript
复制
BGP routing table information for VRF default, address family L2VPN EVPN
BGP table version is xxxxx, Local Router ID is xx.xx.xx.xx
Status: s-suppressed, x-deleted, S-stale, d-dampened, h-history, *-valid, >-best
Path type: i-internal, e-external, c-confed, l-local, a-aggregate, r-redist, I-injected
Origin codes: i - IGP, e - EGP, ? - incomplete, | - multipath, & - backup, 2 - best2

   Network            Next Hop            Metric     LocPrf     Weight Path
Route Distinguisher: xxxxx:xxxxx
* i[x]:[0]:[0]:[xx]:[xxxx.xxxx.xxxx]:[0]:[0.0.0.0]/xxx
                      xx.xx.xx.xx                     000          0 xxxxx xxxxx i
*>i                   xx.xx.xx.xx                     000          0 xxxxx xxxxx i
* i[x]:[0]:[0]:[xx]:[xxxx.xxxx.xxxx]:[0]:[0.0.0.0]/xxx
                      xx.xx.xx.xx                     000          0 xxxxx xxxxx i
*>i                   xx.xx.xx.xx                     000          0 xxxxx xxxxx i
.......

以下是Ansible的\ json选项的结果:

代码语言:javascript
复制
{
  "changed": false,
  "failed": false,
  "stdout": [
    {
      "TABLE_vrf": {
        "ROW_vrf": {
          "TABLE_afi": {
            "ROW_afi": {
              "TABLE_safi": {
                "ROW_safi": {
                  "TABLE_rd": {
                    "ROW_rd": [
                      {
                        "TABLE_prefix": {
                          "ROW_prefix": [
                            {
                              "TABLE_path": {
                                "ROW_path": [
                                  {
                                    "aspath": "xxxxx xxxxx",
                                    "best": "none",
                                    "bestcode": null,
                                    "ipnexthop": "0.0.0.0",
                                    "localpref": "000",
                                    "origin": "i",
                                    "pathnr": "0",
                                    "status": "valid",
                                    "statuscode": "*",
                                    "type": "internal",
                                    "typecode": "i",
                                    "weight": "0"
                                  },
                                  {
                                    "aspath": "xxxxx xxxxx",
                                    "best": "bestpath",
                                    "bestcode": ">",
                                    "ipnexthop": "0.0.0.0",
                                    "localpref": "000",
                                    "origin": "i",
                                    "pathnr": "1",
                                    "status": "valid",
                                    "statuscode": "*",
                                    "type": "internal",
                                    "typecode": "i",
                                    "weight": "0"
                                  }
                                ]
                              },
                              "nonipprefix": "[x]:[0]:[0]:[xx]:[xxxx.xxxx.xxxx]:[0]:[0.0.0.0]/xxx"
                            },

下面是我用Python转换的CLI命令的结果:

代码语言:javascript
复制
{
  "changed": false,
  "failed": false,
  "stdout": [containing the whole block of cli command result with ',' after each line]
}

非常感谢你的帮助,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-04 01:21:22

问:“有办法将CLI命令结果转换为JSON吗?”

答:在你的情况下,最好的选择可能是解析

问:“正在为一个交换机工作,但当我在5个交换机上启动它时……”

答:您为所有交换机并发运行任务local_action。只运行一次并写入所有数据,例如,在字典中。给出了清单和下面的测试操作手册

代码语言:javascript
复制
shell> cat hosts
sw1
sw2
sw3
代码语言:javascript
复制
shell> cat playbook.yml
- name: Playbook
  hosts: all
  gather_facts: false
  tasks:
  - set_fact:
      cli_result:
        stdout:
          - TABLE_vrf:
              ROW_vrf: to be continued
  - copy:
      content: "{{ _dict|to_nice_yaml(indent=2) }}"
      dest: cli_result.json
    delegate_to: localhost
    run_once: true
    vars:
      _keys: "{{ ansible_play_hosts }}"
      _vals: "{{ ansible_play_hosts|
                 map('extract', hostvars, ['cli_result', 'stdout'])|
                 list }}"
      _dict: "{{ dict(_keys|zip(_vals)) }}"

游戏手册创建文件cli_result.json。

代码语言:javascript
复制
sw1:
- TABLE_vrf:
    ROW_vrf: to be continued
sw2:
- TABLE_vrf:
    ROW_vrf: to be continued
sw3:
- TABLE_vrf:
    ROW_vrf: to be continued
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71340019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档