Overview of Using Junos PyEZ Configuration Tables to Define and Configure Structured Resources
Starting in Junos PyEZ Release 2.0, Junos PyEZ enables you to use Tables and Views to configure devices running Junos OS. Tables and Views are defined using simple YAML files that contain key and value pair mappings, so no complex coding is required to create them. You can create Tables and Views that define structured configuration resources. When you add the Table to the Junos PyEZ framework, Junos PyEZ dynamically creates a configuration class for the resource, which you can use to programmatically configure the resource on a device.
To configure devices running Junos OS using configuration Tables and Views, you must identify the resource to model, create the Table and View definitions for that resource, and then use those definitions to configure the resource in your Junos PyEZ application. The general steps are outlined in this topic.
Creating the Structured Resource
To create the structured resource:
- Identify the Junos OS configuration for which you want
to define a structured resource; for example, a
user
object at the[edit system login]
hierarchy level.user@host> show configuration system login | display xml
<rpc-reply> <configuration> <system> <login> <user> <name>jsmith</name> <full-name>J Smith</full-name> <uid>555</uid> <class>super-user</class> <authentication> <encrypted-password>$ABC123</encrypted-password> </authentication> </user> </login> </system> </configuration> ... </rpc-reply>
- Create the Table and View definitions for the structured
resource.
For detailed information about creating configuration Tables and Views, see Defining Junos PyEZ Configuration Tables and Defining Views for Junos PyEZ Configuration Tables.
UserConfigTable: set: system/login/user key-field: username view: UserConfigView UserConfigView: groups: auth: authentication fields: username: name userclass: { class : { 'default' : 'unauthorized' }} uid: { uid: { 'type': 'int', 'default':1001, 'minValue':100, 'maxValue':64000 }} fullname: full-name fields_auth: password: encrypted-password
- Add the structured resource to the Junos PyEZ framework either as an inline string or as an external file, as discussed in Loading Inline or External Tables and Views in Junos PyEZ Applications.
Using the Resource in a Junos PyEZ Application
To configure the resource in your Junos PyEZ application:
- Create a
Device
instance and connect to the device. For example:from jnpr.junos import Device from myTables.ConfigTables import UserConfigTable dev = Device(host='router.example.com').open()
- Create a Table object and associate it with the device.
uc = UserConfigTable(dev)
- Configure the resource by
defining values for the necessary fields, including all key fields
that are defined in the Table’s
key-field
property.For detailed information about configuring the resource, see Using Junos PyEZ Configuration Tables to Configure Structured Resources on Devices Running Junos OS.
uc.username = 'user1' uc.userclass = 'operator' uc.password = '$ABC123'
- Call the
append()
method to build the Junos XML configuration that contains the configuration changes.uc.append()
Note After you call
append()
, the value for each field is reset to its default value or toNone
, if the View does not define a default. If you configure another resource, the initial values for that resource are the reset values rather than the values that were configured for the previous resource. - Repeat Step 3 and Step 4 for each additional resource to configure.
- Load and commit the configuration changes to the shared
configuration database on the device by using one of the following
approaches:
Call the
set()
method, which automatically calls thelock()
,load()
,commit()
, andunlock()
methods.uc.set(merge=True, comment="Junos PyEZ commit")
Call the individual
lock()
,load()
,commit()
, andunlock()
methods.uc.lock() uc.load(merge=True) uc.commit(comment="Junos PyEZ commit") uc.unlock()
- Close the device connection.
dev.close()
For more information about the using the different methods to load and commit the configuration data, see Using Junos PyEZ to Configure Devices Running Junos OS and Using Junos PyEZ to Commit the Configuration.