Ansibleのparse_cli_textfsmフィルターを使ったNWテスト ②事前事後の差分確認


はじめに

前回は、Ansibleを使った事前事後の無影響確認を行いました。

Ansibleのparse_cli_textfsmフィルターを使ったNWテスト ①事前事後の無影響確認

それ以外のテストパターンとして、想定結果は定義せず、単純に事前事後の差分を表示させたい場合もあると思います。

前回と大きな違いはありませんが、備忘録を兼ねて、変更部分をメインにご紹介します。

Playbook

今回は(5)と(6)の間でスタティックルートの追加・削除を行っています(A)。
また、(11)のassertモジュールは使わず、フィルタープラグインの一種であるdifferenceを使って、事前事後のリストの内、事前のみに含まれるもの(B)、事後のみに含まれるもの(C)を抽出しています。
(一発で増減を抽出してくれるのが一番手っ取り早いのですが、ちょっと探した限り見当たりませんでした。。)

playbook2.yml
---

- hosts: cisco
  gather_facts: no
  connection: network_cli

  tasks:

    # 前回の(1)~(5)と同様のため省略

    - name: configure static route #(A)
      ios_config:
        lines:
          - ip route 10.3.0.0 255.255.255.0 192.168.1.202
          - no ip route 10.3.1.0 255.255.255.0 192.168.1.202

    # 前回の(6)~(10)と同様のため省略

    - name: display the list only included in "before" #(B)
      debug:
        msg:
          - "{{ sorted_route_before | difference(sorted_route_after) }}"

    - name: display the list only included in "after" #(C)
      debug:
        msg:
          - "{{ sorted_route_after | difference(sorted_route_before) }}"

  vars:
    extracted_route_before: []
    extracted_route_after: []

出力結果

結果を見ると、ルート削除した10.3.1.0が(B)で表示され、追加した10.3.0.0が(C)で表示されていることが分かります。

[centos@localhost ansible]$ ansible-playbook -i inventory playbook2.yml

PLAY [cisco] *******************************************************************

(省略)

TASK [configure static route] **************************************************
changed: [192.168.1.200]

(省略)

TASK [display the list only included in "before"] ******************************
ok: [192.168.1.200] => {
    "msg": [
        [
            [
                "10.3.1.0", 
                "24", 
                "192.168.1.202", 
                "S"
            ]
        ]
    ]
}

TASK [display the list only included in "after"] *******************************
ok: [192.168.1.200] => {
    "msg": [
        [
            [
                "10.3.0.0", 
                "24", 
                "192.168.1.202", 
                "S"
            ]
        ]
    ]
}

PLAY RECAP *********************************************************************
192.168.1.200              : ok=12   changed=1    unreachable=0    failed=0   

[centos@localhost ansible]$