はじめに
NSO では Plug-and-play scripting という機能でスクリプトをサポートしております。Plug-and-play scripting には 3 つの種類があり、今回は 3 つめのポストコミットクリプトについて説明します。
- Command scripts
- Policy scripts
- Post-commit scripts(今回の内容)
詳細は下記のサイトで説明されていますが、ここでは examples を用いて簡単に試す方法を紹介します。
https://developer.cisco.com/docs/nso/guides/#!plug-and-play-scripting/plug-and-play-scripting
ポストコミットスクリプトとは
ポストコミットスクリプトとは、NSO がトランザクションを実行した後、トランザクションロックが解除される前に実行されるスクリプトです。ポストコミットスクリプトを作成する際には、下記の点に注意する必要があります。
- スクリプトが完了するまで、トランザクションも完了しません
- スクリプト によって、トランザクションの内容を変更することはできません
- スクリプトは "--post-commit" が指定された際、下記出力を stdout に出力する必要があります
begin post-commit
end
Examples とコマンドスクリプトの設置
$NCS_DIR の中には examples.ncs というフォルダがあり、自己学習のためのサンプルがあらかじめ用意されています。そのうち 11 番が Scripting のサンプルになります(NSO 5.7 の場合)。
NSO のローカルインストールの場合、ncs-setup コマンドを使って NSO の実行ディレクトリを作成すると、 scripts/post-commit というフォルダが自動で生成されます。その中にサンプルのファイル show_diff.sh をコピーして設置します。
$ pwd
/home/cisco/ncs-run/scripts/post-commit
$ cp $NCS_DIR/examples.ncs/getting-started/developing-with-ncs/11-scripting/packages/scripting/scripts/post-commit/show_diff.sh .
$ ls
show_diff.sh
このスクリプトは、commit 後の差分をファイルに出力しますが、出力先をあらかじめ環境変数で指定しておく必要があります。これには Linux のシェルから、下記のようなコマンドを実行します。ここでは差分の出力ファイルを /home/cisco/out.txt としています。
$ export TEST_POST_COMMIT_SHOW_DIFF_FILE=/home/cisco/out.txt
ポストコミットクリプトの読み込み
ポストコミットスクリプトを設置したら、NSO に読み込ませます。読み込む際は script reload all コマンドを使います。
admin@ncs# script reload all
/home/cisco/NSO-5.7/scripts: ok
command:
device_save.sh: unchanged
/home/cisco/ncs-run/scripts: ok
post-commit:
show_diff.sh: new
admin@ncs#
無事に show_diff.sh が post-commit スクリプトとして読み込まれました。
ポストコミットスクリプトの動作確認
NSO で設定を変更します。
admin@ncs# config t
Entering configuration mode terminal
admin@ncs(config)# devices authgroups group IOS umap admin remote-name cisco remote-password cisco remote-secondary-password cisco
admin@ncs(config-umap-admin)# commit
Commit complete.
admin@ncs(config-umap-admin)#
ここで出力先のファイルを見ると、コミットされた差分が記録されていることがわかります。
cisco@NSO:~$ cat out.txt
Wed 16 Feb 2022 09:48:45 PM PST
/home/cisco/ncs-run/scripts/post-commit/show_diff.sh
CONFD_MAAPI_USID=50
CONFD_MAAPI_THANDLE=54
--- transaction diff ---
created : /devices/authgroups/group{IOS}
value set : /devices/authgroups/group{IOS}/name
created : /devices/authgroups/group{IOS}/umap{admin}
value set : /devices/authgroups/group{IOS}/umap{admin}/local-user
value set : /devices/authgroups/group{IOS}/umap{admin}/remote-name
value set : /devices/authgroups/group{IOS}/umap{admin}/remote-password
value set : /devices/authgroups/group{IOS}/umap{admin}/remote-secondary-password
cisco@NSO:~$
このようにポストコミットスクリプトを利用することで、トランザクション後にスクリプトよる処理を行うことができるようになります。Commit されたらチャットで通知したり、メールで差分を通知したりなど、使い方次第で様々な応用が考えられます。