このページで
例: コミット・スクリプト・マクロを使用したカスタム構成構文の作成
Junos OS設定には、カスタム設定構文を持つステートメントを含 apply-macro
めることができます。それ自体では、 apply-macro
ステートメントはデバイスに運用上の影響を与えるわけではありません。コミットスクリプトマクロは、カスタム設定構文を処理し、標準的なJunos OS設定ステートメントに展開します。その後、永続的または一時的な変更として追加されます。この例では、コミット スクリプトのマクロを使用してステートメントを検査 apply-macro
し、Junos OS 設定ステートメントを生成する方法を示します。
要件
この例では、以下のハードウェアとソフトウェアのコンポーネントを使用しています。
Junos OS を実行するデバイス。
Python スクリプトを使用する場合、Junos OS リリース 16.1R3 以降のリリース。
概要とコミット スクリプト
表 1 は、カスタム構文を含むマクロと、それに対応する標準的な Junos OS CLI(コマンドライン インターフェイス)構文への拡張を示しています。
カスタム マクロ構文 |
拡張された Junos OS CLI 構文 |
---|---|
protocols { mpls { apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; } } } |
protocols { mpls { admin-groups { blue 0; } label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; } label-switched-path blue-lsp-10.2.2.2 { to 10.2.2.2; admin-group include-any blue; } label-switched-path blue-lsp-10.3.3.3 { to 10.3.3.3; admin-group include-any blue; } label-switched-path blue-lsp-10.4.4.4 { to 10.4.4.4; admin-group include-any blue; } } } |
この例では、Junos OS管理(mgd)プロセスがステートメントを探apply-macro
して設定を検査します。apply-macro
階層レベルに[edit protocols mpls]
パラメータがcolor
含まれている各ステートメントに対して、スクリプトはステートメント内apply-macro
で提供されるデータを使用して一時的な変更を生成し、マクロをLSPの標準的なJunos OS管理グループに展開します。
この例を機能させるには、 apply-macro
階層レベルに[edit protocols mpls]
アドレス、および パラメーターのセットをcolor
group-value
含める必要があります。コミット スクリプトは各アドレスを LSP 設定に変換し、スクリプトは パラメータをcolor
管理グループに変換します。
表 1 のマクロを拡張するコミット スクリプトのインストラクションと、スクリプトの説明を 1 行ずつ説明します。
XSLT 構文
1 <?xml version="1.0" standalone="yes"?> 2 <xsl:stylesheet version="1.0" 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 4 xmlns:junos="http://xml.juniper.net/junos/*/junos" 5 xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" 6 xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> 7 <xsl:import href="../import/junos.xsl"/> 8 <xsl:template match="configuration"> 9 <xsl:variable name="mpls" select="protocols/mpls"/> 10 <xsl:for-each select="$mpls/apply-macro[data/name = 'color']"> 11 <xsl:variable name="color" select="data[name = 'color']/value"/> 12 <xsl:variable name="group-value" select="data[name = \ 'group-value']/value"/> 13 <transient-change> 14 <protocols> 15 <mpls> 16 <admin-groups> 17 <name> 18 <xsl:value-of select="$color"/> 19 </name> 20 <group-value> 21 <xsl:value-of select="$group-value"/> 22 </group-value> 23 </admin-groups> 24 <xsl:for-each select="data[not(value)]/name"> 25 <label-switched-path> 26 <name> 27 <xsl:value-of select="concat($color, '-lsp-', .)"/> 28 </name> 29 <to><xsl:value-of select="."/></to> 30 <admin-group> 31 <include-any> 32 <xsl:value-of select="$color"/> 33 </include-any> 34 </admin-group> 35 </label-switched-path> 36 </xsl:for-each> 37 </mpls> 38 </protocols> 39 </transient-change> 40 </xsl:for-each> 41 </xsl:template> 42 </xsl:stylesheet>
1~8 行目(および行 43 と 44 行)は、XSLT コミット スクリプトのすべてで含まれる定型です。簡潔にするために、ここでは行 1~8 を省略します。
9行目は、 というmpls
変数に [edit protocols mpls]
階層レベルを割り当てます。
9 <xsl:variable name="mpls" select="protocols/mpls"/>
10 行目は、 パラメーターを[edit protocols mpls]
含む階層レベルのすべてのapply-macro
ステートメントをcolor
選択します。表 1 のサンプル構成には、1 つのapply-macro
ステートメントのみが含まれています。したがって、この<xsl:for-each>
プログラミング命令は 1 回だけ有効になります。
10 <xsl:for-each select="$mpls/apply-macro[data/name = 'color']">
11 行目は、この場合blue
は、 というcolor
変数に パラメーターのcolor
値を割り当てます。
11 <xsl:variable name="color" select="data[name = 'color']/value"/>
12 行目は、このケース0
では、 というgroup-value
変数に パラメーターのgroup-value
値を割り当てます。
12 <xsl:variable name="group-value" select="data[name = \ 'group-value']/value"/>
13行目から15行目では、 階層レベルで一時的な変更が生成されます [edit protocols mpls]
。
13 <transient-change> 14 <protocols> 15 <mpls>
16~23行目は、 ステートメントを admin-groups
コンフィギュレーションに追加し、変数の color
値をグループ名に、変数の値を group-value
グループ値に割り当てます。
16 <admin-groups> 17 <name> 18 <xsl:value-of select="$color"/> 19 </name> 20 <group-value> 21 <xsl:value-of select="$group-value"/> 22 </group-value> 23 </admin-groups>
その結果、設定ステートメントは次のようになります。
admin-groups { blue 0; }
24 行目は、割り当てられた値がないすべてのパラメーターの名前を選択します。この場合は 4 つの IP アドレスです。この <xsl:for-each>
プログラミング手順では、マクロを通して再帰を使用し、各 IP アドレスを順番に選択します。 color
および group-value
の各パラメータには、それぞれ(blue
および 0
、)値が割り当てられているため、この行は割り当てません。
24 <xsl:for-each select="data[not(value)]/name">
25行目は、設定に label-switched-path
ステートメントを追加します。
25 <label-switched-path>
26行目から28行目は、変数の値、テキスト --lsp
、および現在行24で選択されている現在のcolor
IPアドレス(" .
で表される)を連結する名前を割り当てますlabel-switched-path
。
26 <name> 27 <xsl:value-of select="concat($color, '-lsp-', .)"/> 28 </name>
29行目は、 to
ステートメントを設定に追加し、その値を現在ライン24で選択されているIPアドレスに設定します。
29 <to><xsl:value-of select="."/></to>
30~34行目は、 admin-group include-any
ステートメントを設定に追加し、その値を 変数の値に color
設定します。
30 <admin-group> 31 <include-any> 32 <xsl:value-of select="$color"/> 33 </include-any> 34 </admin-group>
結果として得られる設定ステートメント(1つのパスの場合)は次のとおりです。
label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; }
35~42行目は終了タグです。
35 </label-switched-path> 36 </xsl:for-each> 37 </mpls> 38 </protocols> 39 </transient-change> 40 </xsl:for-each> 41 </xsl:template> 42 </xsl:stylesheet>
SLAX 構文
同等の SLAX スクリプトは次のとおりです。
version 1.0; ns junos = "http://xml.juniper.net/junos/*/junos"; ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; import "../import/junos.xsl"; match configuration { var $mpls = protocols/mpls; for-each ($mpls/apply-macro[data/name = 'color']) { var $color = data[name = 'color']/value; var $group-value = data[name='group-value']/value; <transient-change> { <protocols> { <mpls> { <admin-groups> { <name> $color; <group-value> $group-value; } for-each (data[not(value)]/name) { <label-switched-path> { <name> $color _ '-lsp-' _ .; <to> .; <admin-group> { <include-any> $color; } } } } } } } }
Python 構文
次のPython構文では、同一の設定変更が行われます。
from junos import Junos_Configuration import jcs def main(): # Get configuration root object root = Junos_Configuration for element in root.xpath("./protocols/mpls/ \ apply-macro[data/name='color']"): color = element.find("data[name='color']/value").text group_value = element.find("data[name='group-value']/value").text lsp_config ="" for element2 in element.xpath("data[not(value)]/name"): lsp_config = lsp_config + """ <label-switched-path> <name>{0}-lsp-{1}</name> <to>{1}</to> <admin-group> <include-any>{0}</include-any> </admin-group> </label-switched-path> """.format(color, element2.text) change_xml = """ <protocols> <mpls> <admin-groups> <name>{0}</name> <group-value>{1}</group-value> </admin-groups> {2} </mpls> </protocols> """.format(color, group_value, lsp_config).strip() jcs.emit_change(change_xml, "transient-change", "xml") if __name__ == '__main__': main()
この例の詳細については、 例: LSP の管理グループの設定を参照してください。
構成
手順
手順
スクリプトをダウンロード、有効化、テストするには、以下の手順にしたがっています。
スクリプトをテキスト ファイルにコピーし、 lsp-admin.xsl、 lsp-admin.slax、または必要に応じて lsp-admin.py ファイルに名前を付け、デバイス上の /var/db/scripts/commit ディレクトリにコピーします。
メモ:符号なしPythonスクリプトは、junos OS
super-user
ログインクラスのルートまたはユーザーのいずれかが所有する必要があり、ファイルの書き込み権限を持つことができるのはファイル所有者だけです。スクリプトが Python で記述されている場合は、符号なし Python スクリプトの実行を有効にします。
[edit] user@host# set system scripts language python
メモ:Python 3を
language python3
使用してPythonスクリプトを実行するように ステートメントを設定するか、 ステートメントでlanguage python
Python 2.7を使用してPythonスクリプトを実行するように設定します。詳細については、 言語を参照してください。以下のテスト構成スタンザを選択し、Ctrl+cを押してクリップボードにコピーします。SLAX または Python バージョンのスクリプトを使用している場合は、 階層レベルでファイル名を
[edit system scripts commit file]
更新します。system { scripts { commit { allow-transients; file lsp-admin.xsl; } } } protocols { mpls { apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; } } }
設定モードで、 コマンドを
load merge terminal
発行して、スタンザをデバイス設定にマージします。[edit] user@host# load merge terminal [Type ^D at a new line to end input] ... Paste the contents of the clipboard here ...
プロンプトで、マウスと貼り付けアイコンを使用して、クリップボードの内容を貼り付けます。
Enter キーを押します。
Ctrl+dを押します。
設定をコミットします。
user@host# commit
検証
スクリプト実行の検証
目的
スクリプトが想定どおりに動作していることを確認します。
アクション
スクリプトによって作成された設定ステートメントを表示するには、 コマンドを show protocols mpls | display commit-scripts
発行します。
[edit] user@host# show protocols mpls | display commit-scripts apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; } admin-groups { blue 0; } label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; } label-switched-path blue-lsp-10.2.2.2 { to 10.2.2.2; admin-group include-any blue; } label-switched-path blue-lsp-10.3.3.3 { to 10.3.3.3; admin-group include-any blue; } label-switched-path blue-lsp-10.4.4.4 { to 10.4.4.4; admin-group include-any blue; }