Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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.

To configure an event policy that invokes the SLAX event script for a given event:

  1. Copy the script into the /var/db/scripts/event directory on the device, and provide a filename that uses the .slax extension, for example, config-change.slax.
  2. Configure the event script.
  3. Configure an event policy that invokes the event script for your specific event, for example:
  4. Commit the configuration.

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.

To configure an event policy that invokes the Python event script for a given event:

  1. Copy the script into the /var/db/scripts/event directory on the device, and provide a filename that uses the .py extension, for example, config-change.py.
    Note:

    Unsigned Python scripts must be owned by either root or a user in the Junos OS super-user login class, and only the file owner can have write permission for the file.

  2. Enable the execution of unsigned Python scripts on the device.
  3. Configure the event script and the user under whose permissions the script is executed.
  4. Configure an event policy that invokes the event script for your specific event, for example:
  5. Commit the configuration.

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.

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: