来自 json 的 Ansible 构建报告

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

PyATS Ansible playbook 输出保存到 json 文件中:

- name: Export variable to file
  copy:
    content: "{{ output }}"
    dest: "report.json"

json内容如下:

{
  "results": [
    {
      "structured": {
        "interfaces": {
          "Port-channel11": {
            "ipv4": {
              "neighbors": {
                "1.1.0.1": {
                  "ip": "1.1.0.1",
                  "link_layer_address": "0000.0000.00ca"
                }
              }
            }
          },
          "GigabitEthernet1": {
            "ipv4": {
              "neighbors": {
                "1.1.1.2": {
                  "ip": "1.1.1.2",
                  "link_layer_address": "0000.0000.00cb"
                },
                "1.1.1.3": {
                  "ip": "1.1.1.3",
                  "link_layer_address": "0000.0000.00cc"
                }
              }
            }
          }
        }
      },
      "item": {
        "key": "vrf1",
        "value": {
          "route_distinguisher": "1:1",
          "interfaces": [
            "GigabitEthernet1",
            "Port-channel11"
          ]
        }
      },
      "ansible_loop_var": "item"
    },
    {
      "structured": {
        "interfaces": {
          "Port-channel2": {
            "ipv4": {
              "neighbors": {
                "2.2.2.200": {
                  "ip": "2.2.2.200",
                  "link_layer_address": "0000.0000.00dd"
                }
              }
            }
          }
        }
      },
      "item": {
        "key": "vrf2",
        "value": {
          "route_distinguisher": "2:2",
          "interfaces": [
            "Port-channel2"
          ]
        }
      },
      "ansible_loop_var": "item"
    }
  ],
  "skipped": false,
  "msg": "All items completed",
  "changed": false
}

我想从此 json 文件创建一个 csv 文件,其中包含以下数据:

{{ ansible_host/inventory_host }},vrf1,Port-channel11,1.1.0.1,0000.0000.00ca
{{ ansible_host/inventory_host }},vrf1,GigabitEthernet1,1.1.1.2,0000.0000.00cb
{{ ansible_host/inventory_host }},vrf1,GigabitEthernet1,1.1.1.3,0000.0000.00cc
{{ ansible_host/inventory_host }},vrf2,Port-channel2,2.2.2.200,0000.0000.00dd

我能达到的最接近的是:

    - name: Set vrflength variable
      set_fact:
        vrflength: "{{ output.results | length }}"

    - name: Set vrfmaxindex variable
      set_fact:
        vrfmaxindex: "{{ (vrflength | int) - 1 }}"

    - name: Create file
      lineinfile:
        insertafter: EOF
        dest: "report1.csv"
        line: "{{ inventory_hostname }},{{ output.results[ item | int ].item.key }},{{ output.results[ item | int ].structured.interfaces }}"
      with_sequence: start=0 end="{{ vrfmaxindex }}"
`device1,vrf1,{"Port-channel11":{"ipv4":{"neighbors":{"1.1.0.1":{"ip":"1.1.0.1","link_layer_address":"0000.0000.00ca"}}}},"GigabitEthernet1":{"ipv4":{"neighbors":{"1.1.1.2":{"ip":"1.1.1.2","link_layer_address":"0000.0000.00cb"},"1.1.1.3":{"ip":"1.1.1.3","link_layer_address":"0000.0000.00cc"}}}}}

device1,vrf2,{"Port-channel2":{"ipv4":{"neighbors":{"2.2.2.200":{"ip":"2.2.2.200","link_layer_address":"0000.0000.00dd"}}}}}`

我无法弄清楚如何构建所需的嵌套循环(对于每个 VRF、每个接口以及每个邻居,在独立的 CSV 行上打印设备名称、VRF 名称、邻居 IP 和 MAC)。

非常感谢任何帮助。

json csv parsing ansible nested-loops
1个回答
0
投票

使用金贾。比如这部剧

- hosts: all

  vars:

    _csv: |
      {% for i in output.results %}
      {% set key=i.item.key %}
      {% for k,v in i.structured.interfaces.items() %}
      {% for n,l in v.ipv4.neighbors.items() %}
      {{ key }},{{ k }},{{ n }},{{ l.link_layer_address }}
      {% endfor %}
      {% endfor %}
      {% endfor %}

  tasks:

    - include_vars:
        file: report.json
        name: output

    - debug:
        var: _csv

给出(删节)

  _csv: |-
    vrf1,Port-channel11,1.1.0.1,0000.0000.00ca
    vrf1,GigabitEthernet1,1.1.1.2,0000.0000.00cb
    vrf1,GigabitEthernet1,1.1.1.3,0000.0000.00cc
    vrf2,Port-channel2,2.2.2.200,0000.0000.00dd
© www.soinside.com 2019 - 2024. All rights reserved.