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

 

Juniper Networks provides support for using Python to manage devices running Junos OS. The Junos PyEZ library provides simple yet powerful methods to perform operational and configuration tasks on devices running Junos OS. This example outlines one method for using the Junos PyEZ jnpr.junos.utils.config.Config utility to load configuration data from a local file on the configuration management server onto a device running Junos OS.

Requirements

This example uses the following hardware and software components:

  • Configuration management server running Python 2.7 or 3.4 and Junos PyEZ Release 2.0 or a later release

  • Device running Junos OS 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 device running Junos OS

Overview

This example presents a Python program 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 program imports the Device class, which handles the connection with the device running Junos OS; 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 devices running Junos OS. 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 NETCONF session and connection are terminated using the close() method. The Python program 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 program also includes code to handle any additional exceptions that might occur.

Configuration

Creating the Configuration Data File

Step-by-Step Procedure

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

  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:

Creating the Junos PyEZ Program

Step-by-Step Procedure

To create a Python program that uses Junos PyEZ to make configuration changes on a device running Junos OS:

  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 program. If the program does not display the intended code, repeat the instructions in this example to correct the program.

Executing the Junos PyEZ Code

Step-by-Step Procedure

To execute the Junos PyEZ code:

  • On the configuration management server, execute the program.

    user@server:~$ python junos-pyez-config.py

Verification

Verifying the Configuration

Purpose

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

Action

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

user@dc1a> show configuration system scripts
user@dc1a> show system commit
user@dc1a> show log messages

Meaning

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

Troubleshooting

Troubleshooting 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:

Troubleshooting 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.

  • Another user made changes to the shared configuration database but has not yet committed the 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 program with a user who has the necessary permissions, or if appropriate, configure the device running Junos OS to give the current user the necessary permissions to make the changes.

Troubleshooting 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 program with a user who has the necessary permissions, or if appropriate, configure the device running Junos OS to give the current user the necessary permissions to make the changes.