Change the Configuration Using an Event Script
You can configure an event policy to change
the configuration in response to an event. Event policies can modify
the configuration by invoking an event script that changes and commits
the configuration or by using the change-configuration
statement
to execute configuration mode commands that change the configuration.
Event scripts provide more flexibility than the change-configuration
statement when modifying the configuration. For example, event scripts
enable you to check for specific conditions, provide the configuration
data in different formats, and specify how to merge the data with
the existing configuration. In certain cases, for example on dual-Routing
Engine devices that have nonstop active routing (NSR) enabled, event
policies can only use event scripts to modify the configuration.
The following sections discuss using event scripts to modify the configuration.
How to Change the Configuration Using a SLAX or XSLT Event Script
SLAX and XSLT event scripts can call the jcs:load-configuration
template to make structured changes to the Junos OS configuration.
You must establish a connection with the target device before invoking
the template to modify the configuration. For information about the
template, see jcs:load-configuration and Change the Configuration Using SLAX and XSLT Scripts.
The following SLAX event script opens a connection to the local
device, calls the jcs:load-configuration
template to modify and commit the configuration, and then closes
the connection. All of the values required for the jcs:load-configuration
template are defined as variables, which are then passed into the
template as arguments.
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 / { <event-script-results> { /* Open a connection to the local device */ var $connection = jcs:open(); /* Define configuration change */ var $configuration-change = <configuration> { <routing-options> { <static> { <route> { <name>"198.51.100.0/24"; <next-hop>"10.1.3.1"; } } } } /* Load and commit the configuration */ var $load-action = "merge"; var $options := { <commit-options> { <log> "Configuration modified through event script"; } } var $results := { call jcs:load-configuration($connection, $action=$load-action, $configuration=$configuration-change, $commit-options=$options); } /* Close the connection */ var $close-results = jcs:close($connection); } }
To configure an event policy that invokes the SLAX event script for a given event:
How to Change the Configuration Using a Python Event Script
Python scripts can use the Junos PyEZ library to make changes to the configuration on
devices running Junos OS. The Junos PyEZ jnpr.junos.utils.config.Config
utility provides instance methods to lock, load, commit, and unlock
the configuration.
The following Python event script connects to the local device and updates and commits the configuration.
from jnpr.junos import Device from jnpr.junos.utils.config import Config import jcs with Device() as dev: with Config(dev) as cu: cu.load("set routing-options static route 198.51.100.0/24 next-hop 10.1.3.1", format="set") cu.commit(comment="Configuration modified through event script", timeout=300)
To configure an event policy that invokes the Python event script for a given event:
For more information about using Junos PyEZ to configure devices running Junos OS, see the Junos PyEZ Developer Guide.
How to Change the Configuration Using Event Scripts on Devices that have Nonstop Active Routing Enabled
When you use an event policy to change the configuration on
a dual Routing Engine device that has nonstop active routing (NSR)
enabled, we recommend that the event policy invoke an event script
that commits the updated configuration on only the primary Routing
Engine. This helps ensure that the update to the configuration and
the subsequent commit operation are successful on both Routing Engines.
The configuration is automatically synchronized to the backup Routing
Engine, because the commit synchronize
statement is configured
at the [edit system]
hierarchy level as part of the NSR
configuration. Alternatively, if you use the change-configuration
statement to modify the configuration, or if the event script does
not commit the change on only the primary Routing Engine, both Routing
Engines might simultaneously attempt to acquire a lock on the configuration
database, thereby causing one or both commits to fail.
To create an event script that only configures and commits the configuration on the primary Routing Engine, include logic that tests whether the current Routing Engine is the primary Routing Engine. If the current Routing Engine is the primary Routing Engine, update the configuration and commit it.
The following SLAX event script connects to the local device and checks whether the current Routing Engine is the primary Routing Engine. If it is the primary Routing Engine, the script updates the configuration and then commits it.
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 / { <event-script-results> { /* Retrieve chassis information */ var $rpc = <get-chassis-inventory>; var $chassis_rpc = jcs:invoke($rpc); var $current_state = $chassis_rpc/chassis/name; /* Open a connection to the local device */ var $connection = jcs:open(); /* Define configuration change */ var $configuration-change = <configuration> { <routing-options> { <static> { <route> { <name>"198.51.100.0/24"; <next-hop>"10.1.3.1"; } } } } /* Load and commit the configuration */ var $load-action = "merge"; var $options := { <commit-options> { <log> "Configuration modified through event script"; } } if ($current_state == "Chassis") { var $results := { call jcs:load-configuration($connection, $action=$load-action, $configuration = $configuration-change, $commit-options=$options); } } /* Close the connection */ var $close-results = jcs:close($connection); } }
Similarly, the following Python event script connects to the local device and only updates and commits the configuration if the current Routing Engine is the primary Routing Engine:
from jnpr.junos import Device from jnpr.junos.utils.config import Config import jcs with Device() as dev: if("master" in dev.facts["current_re"]): with Config(dev) as cu: cu.load("set routing-options static route 198.51.100.0/24 next-hop 10.1.3.1", format="set") cu.commit(comment="Configuration modified through event script", timeout=300)