このページの目次
例: 設定ステートメントの要求と制限
Junos OSのコミットスクリプトは、カスタム構成ルールを適用します。 候補コンフィギュレーション がコミットされると、アクティブな各コミット スクリプトによって検査されます。この例では、コミットスクリプトを使用して、必須および禁止の設定ステートメントを指定しています。
要件
この例では、イーサネット管理インターフェイス fxp0 を持つ Junos OS を実行するデバイスを使用します。
概要とコミット スクリプト
この例では、コミットスクリプトを使用して、必須および禁止の設定ステートメントを指定する方法を示しています。次のコミットスクリプトは、イーサネット管理インタフェース(fxp0)が設定されていることを確認し、インタフェースが不適切に無効になっていることを検出します。また、このスクリプトは、 bgp
ステートメントが 階層レベルに含まれていない場合も検出します [edit protocols]
。いずれの場合も、スクリプトはエラー メッセージを生成し、コミット操作は失敗します。
スクリプトの例は、XSLT 構文と SLAX 構文の両方で示されています。
XSLT 構文
<?xml version="1.0" standalone="yes"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> <xsl:import href="../import/junos.xsl"/> <xsl:template match="configuration"> <xsl:call-template name="error-if-missing"> <xsl:with-param name="must" select="interfaces/interface[name='fxp0']/ unit[name='0']/family/inet/address"/> <xsl:with-param name="statement" select="'interfaces fxp0 unit 0 family inet address'"/> </xsl:call-template> <xsl:call-template name="error-if-present"> <xsl:with-param name="must" select="interfaces/interface[name='fxp0']/disable | interfaces/interface[name='fxp0']/ unit[name='0']/disable"/> <xsl:with-param name="message"> <xsl:text>The fxp0 interface is disabled.</xsl:text> </xsl:with-param> </xsl:call-template> <xsl:call-template name="error-if-missing"> <xsl:with-param name="must" select="protocols/bgp"/> <xsl:with-param name="statement" select="'protocols bgp'"/> </xsl:call-template> </xsl:template> <xsl:template name="error-if-missing"> <xsl:param name="must"/> <xsl:param name="statement" select="'unknown'"/> <xsl:param name="message" select="'missing mandatory configuration statement'"/> <xsl:if test="not($must)"> <xnm:error> <edit-path><xsl:copy-of select="$statement"/></edit-path> <message><xsl:copy-of select="$message"/></message> </xnm:error> </xsl:if> </xsl:template> <xsl:template name="error-if-present"> <xsl:param name="must" select="1"/> <!-- error if param missing --> <xsl:param name="message" select="'invalid configuration statement'"/> <xsl:for-each select="$must"> <xnm:error> <xsl:call-template name="jcs:edit-path"/> <xsl:call-template name="jcs:statement"/> <message><xsl:copy-of select="$message"/></message> </xnm:error> </xsl:for-each> </xsl:template> </xsl:stylesheet>
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 { call error-if-missing($must = interfaces/interface[name='fxp0']/unit[name='0']/family/inet/address, $statement = 'interfaces fxp0 unit 0 family inet address'); call error-if-present($must = interfaces/interface[name='fxp0']/disable | interfaces/interface[name='fxp0']/unit[name='0']/disable) { with $message = { expr "The fxp0 interface is disabled."; } } call error-if-missing($must = protocols/bgp, $statement = 'protocols bgp'); } error-if-missing ($must, $statement = 'unknown', $message = 'missing mandatory configuration statement') { if (not($must)) { <xnm:error> { <edit-path> { copy-of $statement; } <message> { copy-of $message; } } } } error-if-present ($must = 1, $message = 'invalid configuration statement') { for-each ($must) { <xnm:error> { call jcs:edit-path(); call jcs:statement(); <message> { copy-of $message; } } } }
構成
手順
手順
スクリプトをダウンロード、有効化、およびテストするには:
スクリプトをテキスト ファイルにコピーし、必要に応じてファイルに no-nukes.xsl または no-nukes.slax という名前を付けて、デバイスの /var/db/scripts/commit/ ディレクトリにコピーします。
以下のテスト構成スタンザを選択し、Ctrl+c を押してクリップボードにコピーします。
SLAX バージョンのスクリプトを使用している場合は、階層レベルのファイル名
[edit system scripts commit file]
を no-nukes.slax に変更します。system { scripts { commit { file no-nukes.xsl; } } } interfaces { fxp0 { disable; unit 0 { family inet { address 10.0.0.1/24; } } } }
設定モードで、 コマンドを発行
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 ...
プロンプトで、マウスと貼り付けアイコンを使用して、クリップボードの内容を貼り付けます。
実行キーを押します。
Ctrl+dを押します。
設定をコミットします。
user@host# commit
検証
コミット スクリプトの実行の検証
目的
スクリプトが期待どおりに動作することを確認します。
アクション
コマンドの出力commit
を確認します。このスクリプトでは、イーサネット管理インターフェイス(fxp0)が設定され、有効になっており、 ステートメントが [edit protocols]
階層レベルに含まれている必要がありますbgp
。サンプル設定スタンザには fxp0 インターフェイスが含まれますが、無効にします。また、 bgp
ステートメントは 階層レベルでは設定[edit protocols]
されません。スクリプトを実行すると、エラーが生成され、コミット操作が失敗します。コマンドを発行commit
すると、次の出力が表示されます。
[edit] user@host# commit [edit interfaces interface fxp0 disable] 'disable;' The fxp0 interface is disabled. protocols bgp missing mandatory configuration statement error: 2 errors reported by commit scripts error: commit script failure