Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Junos PyEZ Developer Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Overview of Using Junos PyEZ Configuration Tables to Define and Configure Structured Resources

date_range 10-May-24

Junos PyEZ enables you to use Tables and Views to configure Junos devices. 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 Junos devices 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.

Create the Structured Resource

To create the structured resource:

  1. Identify the configuration for which you want to define a structured resource, for example, a user object at the [edit system login] hierarchy level.
    content_copy zoom_out_map
    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>
  2. Create the Table and View definitions for the structured resource.

    For detailed information about creating configuration Tables and Views, see Define Junos PyEZ Configuration Tables and Define Views for Junos PyEZ Configuration Tables.

    content_copy zoom_out_map
    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
    
  3. Add the structured resource to the Junos PyEZ framework either as an inline string or as an external file, as discussed in Load Inline or External Tables and Views in Junos PyEZ Applications.

Use the Resource in a Junos PyEZ Application

To configure the resource in your Junos PyEZ application:

  1. Create a Device instance and connect to the device. For example:
    content_copy zoom_out_map
    from jnpr.junos import Device
    from myTables.ConfigTables import UserConfigTable
    
    dev = Device(host='router.example.com').open()
    
  2. Create a Table object and associate it with the device.
    content_copy zoom_out_map
    uc = UserConfigTable(dev)
    
  3. 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 Use Junos PyEZ Configuration Tables to Configure Structured Resources on Junos Devices.

    content_copy zoom_out_map
    uc.username = 'user1'
    uc.userclass = 'operator'
    uc.password = '$ABC123'
    
  4. Call the append() method to build the Junos XML configuration that contains the configuration changes.
    content_copy zoom_out_map
    uc.append()
    
    Note:

    After you call append(), the value for each field is reset to its default value or to None, 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.

  5. Repeat Step 3 and Step 4 for each additional resource to configure.
  6. 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 the lock(), load(), commit(), and unlock() methods.

      content_copy zoom_out_map
      uc.set(merge=True, comment='Junos PyEZ commit')
      
    • Call the individual lock(), load(), commit(), and unlock() methods.

      content_copy zoom_out_map
      uc.lock()
      uc.load(merge=True)
      uc.commit(comment='Junos PyEZ commit')
      uc.unlock()
      
  7. Close the device connection.
    content_copy zoom_out_map
    dev.close()
    

For more information about the using the different methods to load and commit the configuration data, see Use Junos PyEZ to Configure Junos Devices and Use Junos PyEZ to Commit the Configuration.

footer-navigation