Ansible网络解析器,用于雪端口通道摘要,NXOS命令

时间:2019-06-21 21:10:14

标签: ansible

有Ansible编写的网络解析器,可以正确处理show命令输出。他们创建的其中一些位于

https://github.com/network-automation/ansible-network-parsers

我正在寻找网络解析器来执行这样的命令和输出。让我知道您是否擅长这些方面可以提供帮助。我们需要输出上述GitHub命令中编写的端口列表。谢谢你的帮助。我尝试了几件事。出现大量Yaml错误。

sffc07ssw01# show port-channel summary 

Flags:  D - Down        P - Up in port-channel (members)
        I - Individual  H - Hot-standby (LACP only)
        s - Suspended   r - Module-removed
        S - Switched    R - Routed
        U - Up (port-channel)
        M - Not in use. Min-links not met
--------------------------------------------------------------------------------
Group Port-       Type     Protocol  Member Ports
      Channel
--------------------------------------------------------------------------------
10    Po10(SD)    Eth      NONE      --
11    Po11(SU)    Eth      LACP      Eth1/1(P)    Eth1/2(P)    
12    Po12(SU)    Eth      LACP      Eth1/3(P)    Eth1/4(P)    
100   Po100(SD)   Eth      NONE      --
121   Po121(SD)   Eth      NONE      --
122   Po122(SD)   Eth      NONE      --
123   Po123(SD)   Eth      NONE      --
125   Po125(SD)   Eth      NONE      --

1 个答案:

答案 0 :(得分:0)

好的,我们不必为此编写网络解析器。我想学习有人可以解释网络解析器的工作原理。

对于nxos,当我们使用transport作为cli时,我们会收到上面的输出。但是,如果我们将transport用作network_cli,则输出将已经是字典格式。

ok: [xyz.com] => {
    "pc_output": {
        "changed": false, 
        "failed": false, 
        "stdout": [
            {
                "TABLE_channel": {
                    "ROW_channel": [
                        {
                            "group": 10, 
                            "layer": "S", 
                            "port-channel": "port-channel10", 
                            "prtcl": "NONE", 
                            "status": "D", 
                            "type": "Eth"
                        }, 
                        {
                            "TABLE_member": {
                                "ROW_member": [
                                    {
                                        "port": "Ethernet1/1", 
                                        "port-status": "P"
                                    }, 
                                    {
                                        "port": "Ethernet1/2", 
                                        "port-status": "P"

This is the way I processed it
```yml
item looks like this

interface_port_channel:
 - { interface: Ethernet 2/1, channel_group: 122, mode: active }
 - { interface: Ethernet 2/2, channel_group: 122, mode: active }

---
- name: Interface portchannel info
  debug: msg="args {{item}} "

- name: configure lacp trunk with the given interfaces
  nxos_config: 
    lines:
      - conf t
      - interface {{item.interface}}
      - channel-group {{item.channel_group}} mode {{item.mode}}
      - exit
    provider: "{{ nxos_provider}}"

- name: "Get Port-channel summary"
  nxos_command: commands="show port-channel summary" provider="{{nxos_provider_network_cli}}"
  register: pc_output

- name: "Printing the port channel output"
  debug: var=pc_output

- set_fact: channel_group_info="{{run_pc}}"
  with_items: "{{pc_output.stdout[0].TABLE_channel.ROW_channel}}"
  when: item.channel_group == run_pc.group
  loop_control: 
      loop_var: run_pc

- debug: var=channel_group_info

- assert:
    that:
        - channel_group_info is defined
        - channel_group_info.layer == 'S' 
        - channel_group_info.status == 'U' 
        - channel_group_info.prtcl == "LACP"
    success_msg: "Port is switched"
    fail_msg: "Please check channel_group_info above to make sure Status, protocol and type is correct"

- set_fact: ports_info="{{item}}"
  with_items: "{{channel_group_info.TABLE_member.ROW_member}}"
  when: item.port == ''.join(item.interface.split())

- debug: var=ports_info["port-status"]

- assert:
    that:
      - ports_info is defined 
      - ports_info["port-status"] == 'P'