Skip to content

pyATS で Blitz を使い、YAML だけで自動化を定義する

pyATS を使うことでネットワーク機器の設定や確認を自動化することが出来ます。 PyATS には Quick Trigger (Blitz) という仕組みがあり、ソースコードを書くこと無く、YAML を書くだけでテストや確認を定義することが出来ます。 今回は Ubuntu 22.04LTS 上で Blitz を実行する手順をメモしておきます。

venv 環境の作成

必須ではありませんが、既存環境を汚染しないように venv 環境を作成しておきます。

1
2
3
python3 -m venv .venv &&
echo 'source .venv/bin/activate' > .envrc &&
direnv allow

必要なパッケージをインストールします。

1
2
python3 -m pip install --upgrade pip &&
python3 -m pip install genie 'pyats[full]'

YAML ファイルの準備

Blitz では 2 つのファイルを用意する必要があります。

  1. Testbed (対象機器情報を定義)
  2. Trigger Datafile (自動化内容を定義)

以下に各々のサンプルを記載します。 Testbed & Trigger Datafile ともに pyATS 実行時にファイル指定が出来ますので、ファイル名は任意で構いません。

Testbed (tb.yaml)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
devices:
  1-c8kv:
    connections:
      cli:
        ip: 10.0.0.1
        port: 22
        protocol: ssh
    credentials:
      default:
        username: "admin"
        password: "admin"
    os: iosxe
    type: iosxe
  2-c8kv:
    connections:
      cli:
        ip: 10.0.0.2
        port: 22
        protocol: ssh
    credentials:
      default:
        username: "admin"
        password: "admin"
    os: iosxe
    type: iosxe
  3-c8kv:
    connections:
      cli:
        ip: 10.0.0.3
        port: 22
        protocol: ssh
    credentials:
      default:
        username: "admin"
        password: "admin"
    os: iosxe
    type: iosxe

Trigger Datafile (trigger.yaml)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
TestCase1:
  groups: ["blitz"]
  source:
    pkg: genie.libs.sdk
    class: triggers.blitz.blitz.Blitz
  test_sections:
    - apply_configuration:
        - configure:
            device: 1-c8kv
            command: |
              router ospf 65000
              network 0.0.0.0 0.0.0.0 area 0.0.0.0
        - configure:
            device: 2-c8kv
            command: |
              router ospf 65000
              network 0.0.0.0 0.0.0.0 area 0.0.0.0
        - configure:
            device: 3-c8kv
            command: |
              router ospf 65000
              network 0.0.0.0 0.0.0.0 area 0.0.0.0
        - sleep:
            sleep_time: 10
    - verify_configuration:
        - execute:
            device: 1-c8kv
            command: show ip route
            include:
              - "10.0.99.2/32"
              - "10.0.99.3/32"
        - execute:
            device: 2-c8kv
            command: show ip route
            include:
              - "10.0.99.1/32"
              - "10.0.99.3/32"
        - execute:
            device: 3-c8kv
            command: show ip route
            include:
              - "10.0.99.1/32"
              - "10.0.99.2/32"
    - revert_configuration:
        - configure:
            device: 1-c8kv
            command: |
              no router ospf 65000
        - configure:
            device: 2-c8kv
            command: |
              no router ospf 65000
        - configure:
            device: 3-c8kv
            command: |
              no router ospf 65000
        - sleep:
            sleep_time: 3
    - verify_revert:
        - execute:
            device: 1-c8kv
            command: show ip route
            exclude:
              - "10.0.99.2/32"
              - "10.0.99.3/32"
        - execute:
            device: 2-c8kv
            command: show ip route
            exclude:
              - "10.0.99.1/32"
              - "10.0.99.3/32"
        - execute:
            device: 3-c8kv
            command: show ip route
            exclude:
              - "10.0.99.1/32"
              - "10.0.99.2/32"

pyATS を実行する

pyATS を実行します。

1
genie run --testbed-file tb.yaml --trigger-file trigger.yaml --trigger-groups "And('blitz')"

実行結果のうち、結果部分は以下のように表示されました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    ・
    ・
    ・
2022-12-14T11:32:57: %EASYPY-INFO: +------------------------------------------------------------------------------+
2022-12-14T11:32:57: %EASYPY-INFO: |                             Task Result Summary                              |
2022-12-14T11:32:57: %EASYPY-INFO: +------------------------------------------------------------------------------+
2022-12-14T11:32:57: %EASYPY-INFO: Task-1: genie_testscript.common_setup                                     PASSED
2022-12-14T11:32:57: %EASYPY-INFO: Task-1: genie_testscript.TestCase1.1-c8kv                                 PASSED
2022-12-14T11:32:57: %EASYPY-INFO: Task-1: genie_testscript.common_cleanup                                   PASSED
2022-12-14T11:32:57: %EASYPY-INFO:
2022-12-14T11:32:57: %EASYPY-INFO: +------------------------------------------------------------------------------+
2022-12-14T11:32:57: %EASYPY-INFO: |                             Task Result Details                              |
2022-12-14T11:32:57: %EASYPY-INFO: +------------------------------------------------------------------------------+
2022-12-14T11:32:57: %EASYPY-INFO: Task-1: genie_testscript
2022-12-14T11:32:57: %EASYPY-INFO: |-- common_setup                                                          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- connect                                                           PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- configure                                                        SKIPPED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- configuration_snapshot                                            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- save_bootvar                                                      PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- learn_system_defaults                                             PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- initialize_traffic                                               SKIPPED
2022-12-14T11:32:57: %EASYPY-INFO: |   `-- PostProcessor-1                                                   PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |-- TestCase1.1-c8kv                                                      PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- apply_configuration                                               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1: Starting action configure on device '1-c8kv'          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1.1: Configuring '1-c8kv'                                PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2: Starting action configure on device '2-c8kv'          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2.1: Configuring '2-c8kv'                                PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3: Starting action configure on device '3-c8kv'          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3.1: Configuring '3-c8kv'                                PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   `-- STEP 4: Starting action sleep                                 PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- verify_configuration                                              PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1: Starting action execute on device '1-c8kv'            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1.1: Executing 'show ip route' on '1-c8kv'               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1.1.1: Verify that '10.0.99.2/32' is included in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1.1.2: Verify that '10.0.99.3/32' is included in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2: Starting action execute on device '2-c8kv'            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2.1: Executing 'show ip route' on '2-c8kv'               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2.1.1: Verify that '10.0.99.1/32' is included in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2.1.2: Verify that '10.0.99.3/32' is included in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3: Starting action execute on device '3-c8kv'            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3.1: Executing 'show ip route' on '3-c8kv'               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3.1.1: Verify that '10.0.99.1/32' is included in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   `-- STEP 3.1.2: Verify that '10.0.99.2/32' is included in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |-- revert_configuration                                              PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1: Starting action configure on device '1-c8kv'          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 1.1: Configuring '1-c8kv'                                PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2: Starting action configure on device '2-c8kv'          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 2.1: Configuring '2-c8kv'                                PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3: Starting action configure on device '3-c8kv'          PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   |-- STEP 3.1: Configuring '3-c8kv'                                PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   |   `-- STEP 4: Starting action sleep                                 PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |   `-- verify_revert                                                     PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 1: Starting action execute on device '1-c8kv'            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 1.1: Executing 'show ip route' on '1-c8kv'               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 1.1.1: Verify that '10.0.99.2/32' is excluded in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 1.1.2: Verify that '10.0.99.3/32' is excluded in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 2: Starting action execute on device '2-c8kv'            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 2.1: Executing 'show ip route' on '2-c8kv'               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 2.1.1: Verify that '10.0.99.1/32' is excluded in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 2.1.2: Verify that '10.0.99.3/32' is excluded in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 3: Starting action execute on device '3-c8kv'            PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 3.1: Executing 'show ip route' on '3-c8kv'               PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       |-- STEP 3.1.1: Verify that '10.0.99.1/32' is excluded in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: |       `-- STEP 3.1.2: Verify that '10.0.99.2/32' is excluded in t...    PASSED
2022-12-14T11:32:57: %EASYPY-INFO: `-- common_cleanup                                                        PASSED
2022-12-14T11:32:57: %EASYPY-INFO:     |-- verify_configuration_snapshot                                     PASSED
2022-12-14T11:32:57: %EASYPY-INFO:     |-- stop_traffic                                                     SKIPPED
2022-12-14T11:32:57: %EASYPY-INFO:     `-- PostProcessor-1                                                   PASSED
2022-12-14T11:32:57: %EASYPY-INFO: Sending report email...
2022-12-14T11:32:57: %EASYPY-INFO: Missing SMTP server configuration, or failed to reach/authenticate/send mail. Result notification email failed to send.
2022-12-14T11:32:57: %EASYPY-INFO: Done!

結果を Web ブラウザで確認する

結果は pyats logs view を実行することで Web ブラウザで確認することが出来ます。 但し、オプションを指定しないと「localhost のランダムポート」を Listen するようです。 「全アドレスのポート TCP/8080」を Listen させるには以下のようにオプションを指定して実行します。

1
pyats logs view --host 0.0.0.0 --port 8080

Web ブラウザでの表示例は以下の通りです。

file