Use Junos PyEZ to Configure Junos Devices
SUMMARY You can use the Junos PyEZ Config
utility or Junos PyEZ Tables and
Views 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.
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 |
|
Any part of the configuration |
|
Supports:
|
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:
Use the Junos PyEZ Config Utility to Configure Junos Devices
Example: Use Junos PyEZ to Load Configuration Data from a File
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
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='dc1a.example.com') as dev: cu = Config(dev)
Table Object
from jnpr.junos import Device from myTables.ConfigTables import ServicesConfigTable with Device(host='dc1a.example.com') as dev: sct = ServicesConfigTable(dev)
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:
-
Lock the configuration using
lock()
-
Modify the configuration by performing one of the following actions:
-
Call
load()
when loading a new complete configuration or modifying specific portions of the configuration -
Call
rollback()
to revert to a previously committed configuration, as described in Roll Back the Configuration -
Call
rescue()
to load the rescue configuration, as described in Load the Rescue Configuration
-
-
Commit the configuration using
commit()
, as described in Commit the Configuration and Use Junos PyEZ to Commit the Configuration -
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.
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:
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='dc1a1.example.com') as dev: with Config(dev, mode='private') as cu: cu.load('set system services netconf traceoptions file test.log', format='set') cu.pdiff() cu.commit()
from jnpr.junos import Device from myTables.ConfigTables import ServicesConfigTable with Device(host='dc1a.example.com') as dev: with ServicesConfigTable(dev, mode='private') as sct: sct.ftp = True sct.ssh = True sct.telnet = True sct.append() sct.load() sct.pdiff() sct.commit()
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.
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:
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='router1.example.com') as dev: with Config(dev, mode='ephemeral') as cu: cu.load('set protocols mpls label-switched-path to-hastings to 192.0.2.1', format='set') cu.commit()
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.
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='router1.example.com') as dev: with Config(dev, mode='ephemeral', ephemeral_instance='eph1') as cu: cu.load('set protocols mpls label-switched-path to-hastings to 192.0.2.1', format='set') cu.commit()
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.
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.
Load Operation |
Argument |
Description |
First Supported Junos PyEZ Release |
---|---|---|---|
|
|
Merge the loaded configuration with the existing configuration. |
1.0 |
|
|
Replace the entire configuration with the loaded configuration. |
1.0 |
|
|
Load configuration data from a patch file. |
2.4.0 |
|
– |
Merge the loaded configuration with the existing configuration,
but replace statements in the existing configuration with those that
specify the |
1.0 |
|
|
Load a complete configuration and compare it 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:
with Device(host='dc1a.example.com') as dev: cu = Config(dev) cu.lock()
As a bound property:
with Device(host='dc1a.example.com') as dev: dev.bind( cu=Config ) dev.cu.lock()