CCIE lab対策(6.2. Network Automation)その2
前回デバイスの登録までを検証しました。今回は登録したデバイスに対して、通常のCLI操作と同じことをNSOから操作する検証をします。
1. NSOのモード
NSOは、OS(Linux: Ubuntu)の操作とNSOのCLI操作(NSO CLI)、NSOのconfiguration mode操作があります。各モードの正式名称は公式ドキュメントを参照ください。各モードで出来る事の例とモード遷移コマンドを図にまとめるとこのような感じになります。
2. デバイスとの同期
NSOは管理対象(以降管理デバイス又は単にデバイスと省略します。)のConfigをCDB1に保存しています。このCDBと同期する動作をsync-xxxxといいます。直訳で理解すると分かりやすいと思います。以下イメージ図です。
3. NSO操作
3.1 CDBリフレッシュからのsh run表示
sync-fromを実行しCDBを最新化して、デバイスのrunning-configを表示するまでを操作します。
① デバイス(IOS-XR:N1)のrunning-configをCDBと同期します。
devices sync-from device 【デバイス名】
admin@ncs# devices sync-from device N1 sync-result { device N1 result true } admin@ncs#
② デバイスのrunning-configを表示します。
show running-config devices device 【デバイス名】 config
admin@ncs# show running-config devices device N1 config devices device N1 config admin fpd auto-upgrade disable disk_status_config minor 80 disk_status_config severe 90 disk_status_config critical 95 <snip> ! call-home service active contact smart-licensing profile CiscoTAC-1 active destination transport-method http exit exit ! ! admin@ncs#
③ 長いので必要なものだけを表示させます。
e.g. interface GigabitEthernet 0/0/0/0
大小の文字を判別しているので、tab補完を使うためには表示とおり大文字を入力する必要があります。
admin@ncs# show running-config devices device N1 config interface GigabitEthernet 0/0/0/0 devices device N1 config interface GigabitEthernet 0/0/0/0 ipv4 address 99.1.11.1 255.255.255.0 no shutdown exit ! ! admin@ncs#
e.g. OSPFの定義情報
admin@ncs# show running-config devices device N1 config router ospf devices device N1 config router ospf 1 area 0 interface GigabitEthernet0/0/0/0 cost 10 fast-reroute per-prefix fast-reroute per-prefix ti-lfa enable network point-to-point exit interface GigabitEthernet0/0/0/1 cost 10 fast-reroute per-prefix fast-reroute per-prefix ti-lfa enable network point-to-point exit interface GigabitEthernet0/0/0/2 cost 10 fast-reroute per-prefix fast-reroute per-prefix ti-lfa enable network point-to-point exit interface Loopback0 cost 10 prefix-sid index 1 exit exit segment-routing mpls exit ! ! admin@ncs#
e.g. パイプを使って表示させたいものを更に限定表示させます。
フィルタする文字列が2つ以上ある場合は、"文字列1|文字列2"のように"で囲む必要があります。
admin@ncs# show running-config devices device N1 config router ospf | include "/0|ti-lfa" interface GigabitEthernet0/0/0/0 fast-reroute per-prefix ti-lfa enable interface GigabitEthernet0/0/0/1 fast-reroute per-prefix ti-lfa enable interface GigabitEthernet0/0/0/2 fast-reroute per-prefix ti-lfa enable admin@ncs#
④ 通常表示以外に様々なフォーマットで表示することができます。
e.g. | display xml (Display output as XML)
admin@ncs# show running-config devices device N1 config interface GigabitEthernet 0/0/0/0 | display xml <config xmlns="http://tail-f.com/ns/config/1.0"> <devices xmlns="http://tail-f.com/ns/ncs"> <device> <name>N1</name> <config> <interface xmlns="http://tail-f.com/ned/cisco-ios-xr"> <GigabitEthernet> <id>0/0/0/0</id> <ipv4> <address> <ip>99.1.11.1</ip> <mask>255.255.255.0</mask> </address> </ipv4> </GigabitEthernet> </interface> </config> </device> </devices> </config> admin@ncs#
e.g. | display json (Display output as json)
admin@ncs# show running-config devices device N1 config interface GigabitEthernet 0/0/0/0 | display json { "data": { "tailf-ncs:devices": { "device": [ { "name": "N1", "config": { "tailf-ned-cisco-ios-xr:interface": { "GigabitEthernet": [ { "id": "0/0/0/0", "ipv4": { "address": { "ip": "99.1.11.1", "mask": "255.255.255.0" } } } ] } } } ] } } } admin@ncs#
3.2 configの保存
running-configをOS上に任意のファイル名で保存することができます。
どこのディレクトリからNSO CLIにログインしたのかが重要になります。なぜならrunning-configを保存し、NSO CLIからログアウトした直下のディレクトリにファイルが保存されているからです。
① カレントディレクトリは、"/tmp/Test_directory_to_save_config"です。
pwd
root@chas4:/tmp/Test_directory_to_save_config# pwd /tmp/Test_directory_to_save_config
ncs_cli -u admin -C
root@chas4:/tmp/Test_directory_to_save_config# ncs_cli -u admin -C User admin last logged in 2022-03-19T02:32:36.901207+00:00, to chas4, from 127.0.0.1 using cli-console admin connected from 127.0.0.1 using console on chas4 admin@ncs#
③ デバイス(IOS-XR:N1)のOSPF-configをXML形式で"N1's_OSPF_CONF"というファイル名で保存します。
show running-config devices device 【デバイス名】 config router ospf | display xml | save 【ファイル名】
admin@ncs# show running-config devices device N1 config router ospf | display xml | save N1's_OSPF_CONF admin@ncs#
exit
admin@ncs# exit root@chas4:/tmp/Test_directory_to_save_config#
⑤ 保存したファイルを確認します。
ls -l
root@chas4:/tmp/Test_directory_to_save_config# ls -l total 4 -rw-r--r-- 1 root root 2106 Mar 19 02:40 "N1's_OSPF_CONF" root@chas4:/tmp/Test_directory_to_save_config#
⑥ ファイルの中身を確認します。
cat 【ファイル名】
root@chas4:/tmp/Test_directory_to_save_config# cat N1\'s_OSPF_CONF <config xmlns="http://tail-f.com/ns/config/1.0"> <devices xmlns="http://tail-f.com/ns/ncs"> <device> <name>N1</name> <config> <router xmlns="http://tail-f.com/ned/cisco-ios-xr"> <ospf> <name>1</name> <area> <id>0</id> <interface> <name>GigabitEthernet0/0/0/0</name> <cost>10</cost> <fast-reroute> <per-prefix/> <per-prefix-conf> <per-prefix> <ti-lfa>enable</ti-lfa> </per-prefix> </per-prefix-conf> </fast-reroute> <network>point-to-point</network> </interface> <interface> <name>GigabitEthernet0/0/0/1</name> <cost>10</cost> <fast-reroute> <per-prefix/> <per-prefix-conf> <per-prefix> <ti-lfa>enable</ti-lfa> </per-prefix> </per-prefix-conf> </fast-reroute> <network>point-to-point</network> </interface> <interface> <name>GigabitEthernet0/0/0/2</name> <cost>10</cost> <fast-reroute> <per-prefix/> <per-prefix-conf> <per-prefix> <ti-lfa>enable</ti-lfa> </per-prefix> </per-prefix-conf> </fast-reroute> <network>point-to-point</network> </interface> <interface> <name>Loopback0</name> <cost>10</cost> <prefix-sid> <index>1</index> </prefix-sid> </interface> </area> <segment-routing> <mpls/> </segment-routing> </ospf> </router> </config> </device> </devices> </config> root@chas4:/tmp/Test_directory_to_save_config#
3.3 デバイスのconfig変更
NSOからデバイス(IOS-XR:N1)のinterface GigabitEthernet 0/0/0/0にdescriptionを追記します。
現状は以下の通りdescriptionが無い状態です。
admin@ncs# show running-config devices device N1 config interface GigabitEthernet 0/0/0/0 devices device N1 config interface GigabitEthernet 0/0/0/0 ipv4 address 99.1.11.1 255.255.255.0 no shutdown exit ! ! admin@ncs#
ncs_cli -u admin -C
root@chas4:/nso-5.7/ncsrun# ncs_cli -u admin -C User admin last logged in 2022-03-19T03:22:44.709682+00:00, to chas4, from 127.0.0.1 using cli-console admin connected from 127.0.0.1 using console on chas4 admin@ncs#
② config modeに移行します。
config
admin@ncs# config Entering configuration mode terminal admin@ncs(config)#
devices device N1
admin@ncs(config)# devices device N1 admin@ncs(config-device-N1)#
④ デバイス(IOS-XR:N1)のconfig modeに移行します。
config
admin@ncs(config-device-N1)# config admin@ncs(config-config)#
⑤ interface GigabitEthernet 0/0/0/0にdescriptionを追記します。
admin@ncs(config-config)# interface GigabitEthernet 0/0/0/0 admin@ncs(config-if)# description to N1:Gi0/0/0/1 admin@ncs(config-if)#
⑥ running-configに追記される内容を確認します。
commit dry-run
admin@ncs(config-if)# commit dry-run cli { local-node { data devices { device N1 { config { interface { GigabitEthernet 0/0/0/0 { + description "to N11:Gi0/0/0/1"; } } } } } } } admin@ncs(config-if)#
⑦ running-configに変更を適用します。
commit
admin@ncs(config-if)# commit Commit complete. admin@ncs(config-if)#
⑧ NSO CLImodeに移行します。
end
admin@ncs(config-if)# end admin@ncs#
⑨ descriptionが追記されていることを確認します。
show running-config devices device N1 config interface GigabitEthernet 0/0/0/0
admin@ncs# show running-config devices device N1 config interface GigabitEthernet 0/0/0/0 devices device N1 config interface GigabitEthernet 0/0/0/0 description to N11:Gi0/0/0/1 ipv4 address 99.1.11.1 255.255.255.0 no shutdown exit ! ! admin@ncs#
3.4 コマンドの実行
前回少し検証していいますが、デバイスに対してNSOからコマンドを実行することができます。
① live-status exec live-status execコマンドで使えるコマンドは以下の通りです。
admin@ncs# devices device N1 live-status exec ? Possible completions: any Execute any command on device any-hidden Same as 'any' action except that nothing will be logged. clear Reset functions (deprecated, use 'any') copy Copy from one file to another (deprecated, use 'any') crypto Encryption module (deprecated, use 'any') reload Halt and perform a cold restart (deprecated, use 'any') show Execute show commands (deprecated, use 'any')
e.g. show cef 1.1.1.6/32
devices device 【デバイス名】 live-status exec 【showコマンド等】
admin@ncs# devices device N1 live-status exec show cef 1.1.1.6/32 result Mon Mar 21 01:06:15.434 UTC 1.1.1.6/32, version 1050, internal 0x1000001 0x1 (ptr 0xdf221d8) [1], 0x0 (0xe0e88a8), 0xa28 (0xe9288a8) Updated Mar 21 01:03:43.577 remote adjacency to GigabitEthernet0/0/0/1 Prefix Len 32, traffic index 0, precedence n/a, priority 3 via 99.1.5.5/32, GigabitEthernet0/0/0/1, 16 dependencies, weight 0, class 0 [flags 0x0] path-idx 0 NHID 0x0 [0xf128530 0x0] next hop 99.1.5.5/32 remote adjacency local label 1001012 labels imposed {1005007} RP/0/RP0/CPU0:N1# admin@ncs#
② live-status exec any
live-status execマンドで扱えないコマンドを扱えます。
e.g. traceroute 1.1.1.6(trace 1.1.1.6のように一部省略しても実行可能)
devices device 【デバイス名】 live-status exec any 【コマンド】
admin@ncs# devices device N1 live-status exec any traceroute 1.1.1.6 result Mon Mar 21 01:12:18.946 UTC Type escape sequence to abort. Tracing the route to 1.1.1.6 1 99.1.5.5 [MPLS: Label 1005007 Exp 0] 32 msec 14 msec 15 msec 2 99.4.5.4 [MPLS: Label 1004009 Exp 0] 18 msec 14 msec 15 msec 3 99.3.4.3 [MPLS: Label 1003009 Exp 0] 18 msec 18 msec 15 msec 4 99.2.3.2 [MPLS: Label 1002014 Exp 0] 33 msec 19 msec 18 msec 5 99.2.6.6 21 msec * 16 msec RP/0/RP0/CPU0:N1# admin@ncs#
4. APIによる操作
4.1 Postman
APIのテストを実行するためにPostman(フリー)を使います。
アカウント作成しなくても使えますが、アカウント作成するとフル機能を利用することができます。以下のURLからダウンロードできます。
www.postman.com
また、Postmanの使用方法やAPI、Webアプリケーションについて大変分かりやすい資料があります。録画データもありますので合わせてご視聴ください。
community.cisco.com
Postmanの画面はこんな感じです。簡単な画面の説明を載せています。
4.2 HTTPメソッドとステータスコード
① HTTPメソッドの代表例
Cisco Community Expert Series Community Live WebベースAPI基礎とPostmanの使い方 Page28 より抜粋
② ステータスコードの代表例
Cisco Community Expert Series Community Live WebベースAPI基礎とPostmanの使い方 Page30 より抜粋
4.3 GETによる情報取得
APIを使ってinterfaceの情報を取得します。
① ビルダー(リクエスト)にURLを入力し、HTTPメソッドは”GET”を選択して"Send"をクリックします。
URLに入力したもの(敢えて間違えています。)
http://192.168.2.17:8080/restconf/data/tailf-ncs:devices/device=N1/config/tailf-ned-cisco-ios-xr:interface/GigabitEthernet=0/0/0/0
Community Live : WebベースAPI基礎とPostmanの使い方 の中でも、ステータスコードを確認することが重要です。と繰り返しありました。
ステータスコードを見ればある程度は何故出来ないのかを知ることができます。
② ステータスコードが401(認証が見つからない。若しくは正しくない。)です。
③ Authorizationタブに移動して、"Basic Auth"を選択し"username"と"password"にNSOのログインアカウント情報を入力します。
defaultは、どちらも"admin"です。
④ ビルダー(リクエスト)の"Send"をクリックします。
⑤ ステータスコードが404(リソースが見つからない)です。
URLが認証エラーが無くなったが、URLが無効だというエラーに変わりました。 URLの構文に関してかなり調べていたので間違っていない自信がありました、以下の点を除いては。
URLの一部に"/"を含めたい時に使う
— やすお (@chimay_wh) March 18, 2022
%2F が / と同じになるということを知った喜びたるや!
"/" = %2F
IOS-XRのGigabitEthernet0/0/0/0がPostmanでGET出来なくてどハマりした。
忘れないようにしよ!
⑥ URLを正して、ビルダー(リクエスト)の"Send"をクリックします。
URLに入力したもの(修正済み:GigabitEthernet=0%2F0%2F0%2F0)
http://192.168.2.17:8080/restconf/data/tailf-ncs:devices/device=N1/config/tailf-ned-cisco-ios-xr:interface/GigabitEthernet=0%2F0%2F0%2F0
APIを使ってinterfaceの情報を取得することが出来ました。
次回はサービスの作成とデプロイの検証をします。
最後までお読みいただきありがとうございました!