Use Junos PyEZ to Configure Devices Running Junos OS
Junos PyEZ enables you to make structured and unstructured configuration
changes on devices running Junos OS. 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
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
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 devices running Junos OS,
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
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
Configuration Data Format
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
Any part of the configuration
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
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
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 devices
running Junos OS. After successfully connecting to the device, you
Config or Table object, depending
on your preferred configuration method, and associate it with the
Device object. For example:
from jnpr.junos import Device from jnpr.junos.utils.config import Config with Device(host='dc1a.example.com') as dev: cu = Config(dev)
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
Modify the configuration by performing one of the following actions:
load()when loading a new complete configuration or modifying specific portions of the configuration
Unlock the configuration using
If you use Tables and Views to make structured configuration
changes on a device, you can choose to call the
unlock() methods individually, or
you can call the
set() method, which calls
all of these methods automatically.
load() method performs the same
function for Table objects and
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.
Starting in Junos PyEZ Release 2.0, you can specify the 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
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
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
ephemeral mode. Thus, calling the
in one of these modes results in a
Starting in Junos PyEZ Release 2.1.3, you can 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 devices running Junos OS.
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
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
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
First Supported Junos PyEZ Release
Merge the loaded configuration with the existing configuration.
Replace the entire configuration with the loaded configuration.
Load configuration data from a patch file.
load replace (Default)
Merge the loaded configuration with the existing configuration,
but replace statements in the existing configuration with those that
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.
How to Create the Config or Table Object as a Property of the Device Instance
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()