Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Junos PyEZ to Configure Junos Devices

Junos PyEZ enables you to make structured and unstructured configuration changes on Junos devices. The user account that is used to make configuration changes must have permissions to change the relevant portions of the configuration on each device. If you do not define a user, the user defaults to $USER.

The following sections compare structured and unstructured configuration changes and provide details about the Junos PyEZ configuration process when making unstructured configuration changes using the Config utility or structured configuration changes using Tables and Views.

Understanding Structured and Unstructured Configuration Changes

Unstructured configuration changes, which consist of loading static or templatized configuration data that is formatted as ASCII text, Junos XML elements, Junos OS set commands, or JavaScript Object Notation (JSON), are performed using the jnpr.junos.utils.config.Config utility. In contrast, structured configuration changes use Junos PyEZ configuration Tables and Views to define specific resources to configure, for example, a Junos OS user account. When you add the Table to the Junos PyEZ framework, Junos PyEZ dynamically creates a configuration class for the resource, which enables you to programmatically configure that resource on a device.

When you use the Config utility to make unstructured configuration changes on Junos devices, you can change any portion of the configuration, but you must use one of the accepted formats for the configuration data as well as the correct syntax for that format. Users who are familiar with the supported configuration formats and want the option to modify any portion of the configuration might favor this method for configuration changes. The Config utility also enables you to roll back to a previously committed configuration or load the existing rescue configuration.

Structured configuration changes, on the other hand, require that you create Tables and Views to define specific resources and only enable you to configure the defined resources on the device. When you define a structured resource, you can specify which configuration statements a user can configure for the resource, and you can also define type and constraint checks to ensure that the users supply acceptable values for the data in their Junos PyEZ application. Once a Table and View have been created, they can easily be shared and reused. A Table user can programmatically configure the resource on a device, and the user does not require any knowledge of supported configuration formats or their syntax.

Table 1 summarizes the two methods that Junos PyEZ supports for making configuration changes.

Table 1: Junos PyEZ Structured and Unstructured Configuration Changes

Configuration Change Type

Utility

Scope

Configuration Data Format

Additional Information

Structured

Tables and Views

Limited to the configuration statements defined in the Table and View

Used to make targeted configuration changes

Does not require knowledge of configuration formats or their syntax

Unstructured

jnpr.junos.utils.config.Config class

Any part of the configuration

  • Text

  • JSON

  • Junos OS set commands

  • Junos XML

Supports:

  • loading configuration data from strings, XML objects, local or remote files, or Jinja2 Templates

  • loading the rescue configuration

  • rolling back the configuration to a previous version

This topic discusses the general configuration process and the operations and elements that are common to both configuration methods. For detailed information about performing configuration updates using either the Config utility or Tables and Views, see the documentation specific to that configuration method.

For more information about using the Config utility to make unstructured configuration changes, see the following topics:

For more information about using configuration Tables and Views to make structured configuration changes, see the following topics:

Understanding the General Configuration Process

Junos PyEZ enables you to make configuration changes on Junos devices. After successfully connecting to the device, you create a Config or Table object, depending on your preferred configuration method, and associate it with the Device object. For example:

Config Object

Table Object

By default, Junos PyEZ updates the candidate global configuration (also known as the shared configuration database). The basic process for making configuration changes is to lock the configuration database, load the configuration changes, commit the configuration to make it active, and then unlock the configuration database. When you use the Junos PyEZ Config utility to make unstructured configuration changes in the shared configuration database, you can perform these actions by calling the appropriate instance methods outlined here:

  1. Lock the configuration using lock()

  2. Modify the configuration by performing one of the following actions:

  3. Commit the configuration using commit() , as described in Commit the Configuration and Use Junos PyEZ to Commit the Configuration

  4. Unlock the configuration using unlock()

If you use Tables and Views to make structured configuration changes on a device, you can choose to call the lock(), load(), commit(), and unlock() methods individually, or you can call the set() method, which calls all of these methods automatically.

Note:

The load() method performs the same function for Table objects and Config objects, but you supply different parameters depending on which object type calls the method.

How to Specify the Configuration Mode

By default, Junos PyEZ updates the candidate global configuration. You can also specify a different configuration mode to use when modifying the configuration database. To specify a mode other than the default, you must create the Config or Table object using a context manager (with ... as syntax) and set the mode argument to the desired mode. Supported modes include private, exclusive, dynamic, batch, and ephemeral.

When you specify a mode other than the default, the context manager handles opening and locking and closing and unlocking the database. This ensures that you do not unintentionally leave the database in a locked state. In these cases, you only need to call the load() and commit() methods to configure the device.

The following examples make configuration changes using the configure private mode:

Note:

The context manager handles opening and locking the configuration database in private, exclusive, dynamic, batch, or ephemeral mode. Thus, calling the lock() or set() methods in one of these modes results in a LockError exception.

Junos PyEZ enables you to update the ephemeral configuration database on devices that support this database. The ephemeral database is an alternate configuration database that provides a fast programmatic interface for performing configuration updates on Junos devices.

Note:

The ephemeral configuration database is an advanced feature which if used incorrectly can have a serious negative impact on the operation of the device. For more information, see Understanding the Ephemeral Configuration Database.

To open and configure the default instance of the ephemeral configuration database, include the mode='ephemeral' argument. For example:

To open and configure a user-defined instance of the ephemeral configuration database, include the mode='ephemeral' argument, and set the ephemeral_instance argument to the name of the instance.

How to Specify the Load Operation

In Junos PyEZ, you can load configuration changes using many of the same load operations that are supported in the Junos OS CLI. You specify the desired load operation by including or omitting the appropriate parameters in the set() method when making structured configuration changes using Tables and Views, or in the load() method for either structured or unstructured configuration changes. Table 2 summarizes the parameter settings required for each type of load operation.

Note:

Because the load override and load update operations require a complete configuration, the overwrite=True and update=True arguments must not be used when making configuration changes using Tables, which only modify specific statements in the configuration.

Table 2: Parameters for Specifying the Load Operation Type in the load() and set() Methods

Load Operation

Argument

Description

First Supported Junos PyEZ Release

load merge

merge=True

Merge the loaded configuration with the existing configuration.

1.0

load override

overwrite=True

Replace the entire configuration with the loaded configuration.

1.0

load patch

patch=True

Load configuration data from a patch file.

2.4.0

load replace (Default)

Merge the loaded configuration with the existing configuration, but replace statements in the existing configuration with those that specify the replace: tag in the loaded configuration. If there is no statement in the existing configuration, the statement in the loaded configuration is added.

1.0

load update

update=True

Compare the complete loaded configuration against the existing configuration. Each configuration element that is different in the loaded configuration replaces its corresponding element in the existing configuration. During the commit operation, only system processes that are affected by changed configuration elements parse the new configuration.

2.1.0

How to Create the Config or Table Object as a Property of the Device Instance

The Device class bind() method enables you to attach various instances and methods to the Device instance. In your Junos PyEZ application, you have the option to bind the Config or Table object to the Device instance. The functionality of the methods does not change, but the method execution differs slightly. For example:

As a standalone variable:

As a bound property: