てくなべ (tekunabe)

ansible / network / automation

[Ansible] Junos の interface の状態を debug で表示する(display json 編)

はじめに

以前の記事で、gather_facts を利用して Junos のインターフェースの状態を取得して debug で表示するという Playbook をご紹介しました。

tekunabe.hatenablog.jp

今回は普通に show interfaces terse コマンドを利用します。ただし、せっかく JSON で表示してくれるバージョンの Junos であれば、display json を使うのも手です。

この記事では、show interfaces terse | display json コマンド相当の処理を利用します。

動作確認環境


Playbook

Playbook はこんな感じです。

- name: Junos Module TEST
  hosts: junos
  gather_facts: no      # ポイント(1)
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: show
      junos_command:
        commands:
          - show interfaces terse 
        display: json               # ポイント(2)
      register: result

    - name: debug
      debug:
        msg: "admin_status : {{ admin_status }} / oper_status : {{ oper_status }}"
      loop: "{{ result.stdout[0]['interface-information'][0]['physical-interface'] }}"  # ポイント(3)
      vars:
        admin_status: "{{ item['admin-status'][0]['data'] }}"      # ポイント(4)
        oper_status: "{{ item['oper-status'][0]['data'] }}"

ポイント・相違点

ポイント、オリジナルとの相違点は以下のとおりです。

ポイント(1)

gather_facts ではなく、show コマンドを利用するため、無効にします。

ポイント(2)

junos_command モジュールを利用して、show interfaces terse コマンドを実行します。

json で結果を取得したいので、display: json を指定します。

ポイント(3)

show interfaces terse コマンドの結果を json で取得して、物理インターフェースの情報がある構造を指定します。 ここまで指定すると、各インターフェースの情報がリストで格納されているので、witth_dict ではなく普通に loop を使います。

機種は設定によっては、ここの指定方法が変わるかもしれません。

なお、json の全内容は以下の通りです。(1000行以上あるのご注意)

クリック指定開く

{
    "interface-information": [
        {
            "physical-interface": [
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "vpls"
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "lc-0/0/0.32769"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "lc-0/0/0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ]
                                },
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet6"
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "pfe-0/0/0.16383"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "pfe-0/0/0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "pfh-0/0/0.16383"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        },
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "pfh-0/0/0.16384"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "pfh-0/0/0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/1"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/2"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/3"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/4"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/5"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/6"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ge-0/0/7"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "down"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "cbp0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "demux0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "dsc"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ],
                                    "interface-address": [
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "10.0.0.4/8"
                                                }
                                            ]
                                        },
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "128.0.0.1/2"
                                                }
                                            ]
                                        },
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "128.0.0.4/2"
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet6"
                                        }
                                    ],
                                    "interface-address": [
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "fe80::250:56ff:fea2:f74b/64"
                                                }
                                            ]
                                        },
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "fec0::a:0:0:4/64"
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "address-family-name": [
                                        {
                                            "data": "tnp"
                                        }
                                    ],
                                    "interface-address": [
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "0x4"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "em1.0"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "em1"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "esi"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti1"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti2"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti3"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti4"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti5"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti6"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "fti7"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ],
                                    "interface-address": [
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "100.123.1.0/16"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "fxp0.0"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "fxp0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "gre"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "ipip"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "irb"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ],
                                    "interface-address": [
                                        {
                                            "ifa-local": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "128.0.0.127/2"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "jsrv.1"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "jsrv"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "logical-interface": [
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ],
                                    "interface-address": [
                                        {
                                            "ifa-destination": [
                                                {
                                                    "attributes": {
                                                        "junos:emit": "emit"
                                                    },
                                                    "data": "0/0"
                                                }
                                            ],
                                            "ifa-local": [
                                                {
                                                    "data": "127.0.0.1"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "lo0.16384"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        },
                        {
                            "address-family": [
                                {
                                    "address-family-name": [
                                        {
                                            "data": "inet"
                                        }
                                    ]
                                }
                            ],
                            "admin-status": [
                                {
                                    "data": "up"
                                }
                            ],
                            "filter-information": [
                                {}
                            ],
                            "name": [
                                {
                                    "data": "lo0.16385"
                                }
                            ],
                            "oper-status": [
                                {
                                    "data": "up"
                                }
                            ]
                        }
                    ],
                    "name": [
                        {
                            "data": "lo0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "lsi"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "mtun"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "pimd"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "pime"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "pip0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "pp0"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "rbeb"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "tap"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                },
                {
                    "admin-status": [
                        {
                            "data": "up"
                        }
                    ],
                    "name": [
                        {
                            "data": "vtep"
                        }
                    ],
                    "oper-status": [
                        {
                            "data": "up"
                        }
                    ]
                }
            ]
        }
    ]
}

ポイント(4)

json の構造に沿って、インターフェース名と各状態をタスク変数(ループごとに更新される)に格納します。

実行結果

Playbook の実行結果です。

$ ansible-playbook -i hosts fact_test2.yml

PLAY [Junos Module TEST] *************************************************************************************************************

TASK [show] **************************************************************************************************************************
ok: [vmx]

TASK [debug] *************************************************************************************************************************
ok: [vmx] => (item={'name': [{'data': 'ge-0/0/0'}], 'admin-status': [{'data': 'up'}], 'oper-status': [{'data': 'down'}]}) => {
    "msg": "admin_status : up / oper_status : down"
}
ok: [vmx] => (item={'name': [{'data': 'lc-0/0/0'}], 'admin-status': [{'data': 'up'}], 'oper-status': [{'data': 'up'}], 'logical-interface': [{'name': [{'data': 'lc-0/0/0.32769'}], 'admin-status': [{'data': 'up'}], 'oper-status': [{'data': 'up'}], 'filter-information': [{}], 'address-family': [{'address-family-name': [{'data': 'vpls'}]}]}]}) => {
    "msg": "admin_status : up / oper_status : up"
}
ok: [vmx] => (item={'name': [{'data': 'pfe-0/0/0'}], 'admin-status': [{'data': 'up'}], 'oper-status': [{'data': 'up'}], 'logical-interface': [{'name': [{'data': 'pfe-0/0/0.16383'}], 'admin-status': [{'data': 'up'}], 'oper-status': [{'data': 'up'}], 'filter-information': [{}], 'address-family': [{'address-family-name': [{'data': 'inet'}]}, {'address-family-name': [{'data': 'inet6'}]}]}]}) => {
    "msg": "admin_status : up / oper_status : up"
}

...(略)...

PLAY RECAP ********************************************************************************************************
vmx                        : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

補足

ループごとに表示される変数の値を調整したい場合は、loop_controllabel を使うと便利です。

# ...(略)
    - name: debug
      debug:
        msg: "admin_status : {{ admin_status }} / oper_status : {{ oper_status }}"
      loop: "{{ result.stdout[0]['interface-information'][0]['physical-interface'] }}" 
      vars:
        admin_status: "{{ item['admin-status'][0]['data'] }}"
        oper_status: "{{ item['oper-status'][0]['data'] }}"
      loop_control:           # ポイント
        label: "{{ item['name'][0]['data'] }}"      # ポイント

結果

ok: [vmx] => (item=ge-0/0/0) => {
    "msg": "admin_status : up / oper_status : down"
}


おわりに

状態を取得するだけでも色々な方法があることが分かりますね。

他にも TextFSM をパーサーを利用する方法もあります。

参考

qiita.com tekunabe.hatenablog.jp