Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Example: Use Junos PyEZ to Load Configuration Data from a File

The Junos PyEZ library enables you to perform operational and configuration tasks on Junos devices. This example uses the Junos PyEZ jnpr.junos.utils.config.Config utility to load configuration data from a local file on the configuration management server onto a Junos device.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Python 3.5 or later and Junos PyEZ Release 2.0 or later

  • Junos device with NETCONF enabled and a user account configured with appropriate permissions

  • SSH public/private key pair configured for the appropriate user on the server and Junos device

Overview

This example presents a Python application that uses the Junos PyEZ Config utility to enable a new op script in the configuration of the specified device. The junos-config-add-op-script.conf file, which is located on the configuration management server, contains the relevant configuration data formatted as ASCII text.

The Python application imports the Device class, which handles the connection with the Junos device; the Config class, which is used to make unstructured configuration changes on the target device; and required exceptions from the jnpr.junos.exception module, which contains exceptions encountered when managing Junos devices. This example binds the Config instance to the Device instance rather than creating a standalone variable for the instance of the Config class.

After creating the Device instance for the target device, the open() method establishes a connection and NETCONF session with the device. The Config utility methods then lock the candidate configuration, load the configuration changes into the candidate configuration as a load merge operation, commit the candidate configuration, and then unlock it.

The load() method path parameter is set to the path of the configuration file. Because the configuration file extension indicates the format of the configuration data, the format parameter is omitted from the argument list. Setting merge=True indicates that the device should perform a load merge operation.

After the configuration operations are complete, the application calls the close() method to terminate the NETCONF session and connection. The application includes code for handling exceptions such as LockError for errors that occur when locking the configuration and CommitError for errors that occur during the commit operation. The application also includes code to handle any additional exceptions that might occur.

Configuration

Create the Configuration Data File

Step-by-Step Procedure

To create the configuration data file that is used by the Junos PyEZ application:

  1. Create a new file with the appropriate extension based on the format of the configuration data, which in this example is ASCII text.

  2. Include the desired configuration changes in the file, for example:

Create the Junos PyEZ Application

Step-by-Step Procedure

To create a Python application that uses Junos PyEZ to make configuration changes on a Junos device:

  1. Import any required modules, classes, and objects.

  2. Include any required variables, which for this example includes the hostname of the managed device and the path to the file containing the configuration data.

  3. Create a main() function definition and function call, and place the remaining statements within the definition.

  4. Create an instance of the Device class, and supply the hostname and any parameters required for that specific connection.

    Then open a connection and establish a NETCONF session with the device.

  5. Bind the Config instance to the Device instance.

  6. Lock the configuration.

  7. Load the configuration changes and handle any errors.

  8. Commit the configuration.

  9. Unlock the configuration.

  10. End the NETCONF session and close the connection with the device.

Results

On the configuration management server, review the completed application. If the application does not display the intended code, repeat the instructions in this example to correct the application.

Execute the Junos PyEZ Application

Execute the Application

  • On the configuration management server, execute the application.

Verification

Verify the Configuration

Purpose

Verify that the configuration was correctly updated on the Junos device.

Action

Log in to the Junos device and view the configuration, commit history, and log files to verify the configuration and commit. For example:

Meaning

The configuration and the log file contents indicate that the correct configuration statements were successfully configured and committed on the device.

Troubleshooting

Troubleshoot Timeout Errors

Problem

The Junos PyEZ code generates an RpcTimeoutError message or a TimeoutExpiredError message and fails to update the device configuration.

The default time for a NETCONF RPC to time out is 30 seconds. Large configuration changes might exceed this value causing the operation to time out before the configuration can be uploaded and committed.

Solution

To accommodate configuration changes that might require a commit time that is longer than the default timeout interval, set the timeout interval to an appropriate value and rerun the code. To configure the interval, either set the Device timeout property to an appropriate value, or include the timeout=seconds argument when you call the commit() method to commit the configuration data on a device. For example:

Troubleshoot Configuration Lock Errors

Problem

The Junos PyEZ code generates a LockError message indicating that the configuration cannot be locked. For example:

A configuration lock error can occur for the following reasons:

  • Another user has an exclusive lock on the configuration.

  • The shared configuration database has uncommitted changes.

  • The user executing the Junos PyEZ code does not have permissions to configure the device.

Solution

If another user has an exclusive lock on the configuration or has modified the configuration, wait until the lock is released or the changes are committed, and execute the code again. If the cause of the issue is that the user does not have permissions to configure the device, either execute the application with a user who has the necessary permissions, or if appropriate, configure the Junos device to give the current user the necessary permissions to make the changes.

Troubleshoot Configuration Change Errors

Problem

The Junos PyEZ code generates a ConfigLoadError message indicating that the configuration cannot be modified due to a permissions issue.

This error message might be generated when the user executing the Junos PyEZ code has permission to alter the configuration, but does not have permission to alter the desired portion of the configuration.

Solution

Either execute the application with a user who has the necessary permissions, or if appropriate, configure the Junos device to give the current user the necessary permissions to make the changes.