来自 json 的 Ansible 构建报告

Ansible build report from json

提问人:everlearning777 提问时间:11/15/2023 更新时间:11/15/2023 访问量:19

问:

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 解析 ansible 嵌套循环

答:

1赞 Vladimir Botka 11/15/2023 #1

使用Jinja。例如,该剧

- 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