Ansible-从JSON嵌套字典中提取数据进行分析

时间:2019-03-05 17:00:55

标签: python dictionary ansible

我有一个Ansible解析器,它将一些CLI输出解析为下面的JSON,下面仅是一个路由器的示例

{
    "ansible_facts": {
        "bgp_summary_facts": [
            {
                "65001_10.10.10.1": {
                    "Data": {
                        "inq": "0",
                        "msgrcvd": "638",
                        "msgsent": "424",
                        "neighbor": "10.10.10.1",
                        "uptime": "01:35:54",
                        "peer_as": "65001",
                        "state": "2",
                        "tblver": "0",
                        "version": "4"
                    }
                }
            },
            {
                "65001_10.10.10.2": {
                    "Data": {
                        "inq": "0",
                        "msgrcvd": "208364",
                        "msgsent": "424",
                        "neighbor": "10.10.10.2",
                        "uptime": "3w1d",
                        "peer_as": "65001",
                        "state": "71",
                        "tblver": "0",
                        "version": "4"
                    }
                }
            },
            {
                "65002_10.10.20.1": {
                    "Data": {
                        "inq": "0",
                        "msgrcvd": "0",
                        "msgsent": "1",
                        "neighbor": "10.10.20.1",
                        "uptime": "never",
                        "peer_as": "65002",
                        "state": "Idle",
                        "tblver": "0",
                        "version": "4"
                    }
                }
            },
            {
                "65010": {
                    "Data": {
                        "inq": "0",
                        "msgrcvd": "22611",
                        "msgsent": "424",
                        "neighbor": "10.10.30.1",
                        "uptime": "2d11h",
                        "peer_as": "65010",
                        "state": "36",
                        "tblver": "0",
                        "version": "4"
                    }
                }
            }
        ]
    },
    "changed": false,
    "included": [
        "parsers/ios/show_ip_bgp_summary.yml"
    ]
}

现在,我一直在绞尽脑汁,搜索并阅读了几天的教程,以了解如何现在可以提取这些数据进行分析

我的剧本目前以此结尾,使用我以为id尝试使用JMESPath使其有效的教程,我尝试了多种格式,我还尝试了遍历字典项,我试图将解析器转换为使用列表字典,而不是字典词典,令我沮丧的是,我什么都没得到。

我的最终目标是在循环中处理每个字典以进行分析,最简单的一个是输出哪些同伴掉线(状态没有整数)

   - name: PROCESS RESULTS
     debug:
       msg: "{{ item }}"
     with_items: "{{ bgp_summary_facts | json_query('*.neighbor') }}"

最终目标样本:

RTR-01
neighbour 10.10.20.1 is down
RTR-02
neighbour 10.30.20.1 is down

RTR-01

| neighbour  | bgp as | prefixes | uptime   |
|------------|--------|----------|----------|
| 10.10.10.1 | 65001  | 2        | 01:35:54 |
| 10.10.10.2 | 65001  | 2        | 3w1d     |
| 10.10.20.1 | 65002  |          | never    |
每个路由器的

以此类推

2 个答案:

答案 0 :(得分:1)

如果我对要求的要求正确,这是解析var并获取列出的“决定”的一种方法。

包含测试变量的PB:

---
- hosts: localhost
  gather_facts: false
  vars:
    test_var:
      bgp_summary_facts:
      - 65001_10.10.10.1:
          Data:
            inq: "0"
            msgrcvd: "638"
            msgsent: "424"
            neighbor: 10.10.10.1
            uptime: "01:35:54"
            peer_as: "65001"
            state: "2"
            tblver: "0"
            version: "4"
      - 65001_10.10.10.2:
          Data:
            inq: "0"
            msgrcvd: "208364"
            msgsent: "424"
            neighbor: 10.10.10.2
            uptime: 3w1d
            peer_as: "65001"
            state: "71"
            tblver: "0"
            version: "4"
      - 65002_10.10.20.1:
          Data:
            inq: "0"
            msgrcvd: "0"
            msgsent: "1"
            neighbor: 10.10.20.1
            uptime: never
            peer_as: "65002"
            state: Idle
            tblver: "0"
            version: "4"
      - "65010":
          Data:
            inq: "0"
            msgrcvd: "22611"
            msgsent: "424"
            neighbor: 10.10.30.1
            uptime: 2d11h
            peer_as: "65010"
            state: "36"
            tblver: "0"
            version: "4"
    changed: false
    included:
    - parsers/ios/show_ip_bgp_summary.yml


  tasks:
  - name: print var
    debug:
      msg: "bgp: {{ item.keys() | first }}, Neighbor: {{ item[item.keys() | first].Data['neighbor'] }}, uptime: {{ item[item.keys() | first].Data['uptime'] }}"
    with_items: "{{ test_var.bgp_summary_facts }}"

  - name: print var is down
    debug:
      msg: "is down"
    when: item[item.keys() | first].Data['state'] | int == false
    with_items: "{{ test_var.bgp_summary_facts }}"

为方便起见,我编写了2个debug任务。如果要像示例中那样生成表,则可能应该执行jinja模板任务。

结果:

[http_offline@greenhat-29 tests]$ ansible-playbook  test.yml 

PLAY [localhost] *******************************************************************************************************************************************************************************************************

TASK [print var] *******************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'65001_10.10.10.1': {'Data': {'inq': '0', 'msgrcvd': '638', 'msgsent': '424', 'neighbor': '10.10.10.1', 'uptime': '01:35:54', 'peer_as': '65001', 'state': '2', 'tblver': '0', 'version': '4'}}}) => {
    "msg": "bgp: 65001_10.10.10.1, Neighbor: 10.10.10.1, uptime: 01:35:54"
}
ok: [localhost] => (item={'65001_10.10.10.2': {'Data': {'inq': '0', 'msgrcvd': '208364', 'msgsent': '424', 'neighbor': '10.10.10.2', 'uptime': '3w1d', 'peer_as': '65001', 'state': '71', 'tblver': '0', 'version': '4'}}}) => {
    "msg": "bgp: 65001_10.10.10.2, Neighbor: 10.10.10.2, uptime: 3w1d"
}
ok: [localhost] => (item={'65002_10.10.20.1': {'Data': {'inq': '0', 'msgrcvd': '0', 'msgsent': '1', 'neighbor': '10.10.20.1', 'uptime': 'never', 'peer_as': '65002', 'state': 'Idle', 'tblver': '0', 'version': '4'}}}) => {
    "msg": "bgp: 65002_10.10.20.1, Neighbor: 10.10.20.1, uptime: never"
}
ok: [localhost] => (item={'65010': {'Data': {'inq': '0', 'msgrcvd': '22611', 'msgsent': '424', 'neighbor': '10.10.30.1', 'uptime': '2d11h', 'peer_as': '65010', 'state': '36', 'tblver': '0', 'version': '4'}}}) => {
    "msg": "bgp: 65010, Neighbor: 10.10.30.1, uptime: 2d11h"
}

TASK [print var is down] ***********************************************************************************************************************************************************************************************
skipping: [localhost] => (item={'65001_10.10.10.1': {'Data': {'inq': '0', 'msgrcvd': '638', 'msgsent': '424', 'neighbor': '10.10.10.1', 'uptime': '01:35:54', 'peer_as': '65001', 'state': '2', 'tblver': '0', 'version': '4'}}}) 
skipping: [localhost] => (item={'65001_10.10.10.2': {'Data': {'inq': '0', 'msgrcvd': '208364', 'msgsent': '424', 'neighbor': '10.10.10.2', 'uptime': '3w1d', 'peer_as': '65001', 'state': '71', 'tblver': '0', 'version': '4'}}}) 
ok: [localhost] => (item={'65002_10.10.20.1': {'Data': {'inq': '0', 'msgrcvd': '0', 'msgsent': '1', 'neighbor': '10.10.20.1', 'uptime': 'never', 'peer_as': '65002', 'state': 'Idle', 'tblver': '0', 'version': '4'}}}) => {
    "msg": "is down"
}
skipping: [localhost] => (item={'65010': {'Data': {'inq': '0', 'msgrcvd': '22611', 'msgsent': '424', 'neighbor': '10.10.30.1', 'uptime': '2d11h', 'peer_as': '65010', 'state': '36', 'tblver': '0', 'version': '4'}}}) 

PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   

[http_offline@greenhat-29 tests]$ 

希望有帮助

答案 1 :(得分:1)

我已经为上面创建了jinja2模板。输出结果类似,但我对条件有些困惑,但想在这里分享。

如果您可以举例说明就很好了。


剧本:

- name: hosts
  hosts: localhost
  tasks:
    - name: include vars
      include_vars: vars_t4.yml

    - name: template module
      template:
        src: template/source.json
        dest: target/final.json

模板

template:{% for i in bgp_summary_facts %}
{% for a in  i | dict2items  %}
RTR-{{ a.value.Data.inq }}
neighbour {{ a.value.Data.neighbor }} is down
{% endfor %}
{% endfor %}

输出

RTR-0
neighbour 10.10.10.1 is down

RTR-0
neighbour 10.10.10.2 is down

RTR-0
neighbour 10.10.20.1 is down

RTR-0
neighbour 10.10.30.1 is down