Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Load Inline or External Tables and Views in Junos PyEZ Applications

SUMMARY Import predefined Tables or inline or external custom Tables into your Junos PyEZ application.

Junos PyEZ Tables and Views provide a simple and efficient way to configure Junos devices or extract specific information from operational command output or configuration data. Junos PyEZ provides a set of predefined operational Tables and Views that you can use in applications, or you can create your own custom operational or configuration Tables and Views.

You can create quick inline Tables and Views as a multiline string directly in the Python application, or you can create one or more Table and View definitions in external files and import the Tables into your Python application. Inline Tables and Views are simpler to use, but using external files enables you to create a central, reusable library.

To use Junos PyEZ’s predefined Tables and Views in your Python application, you must import the Table into your application. To use custom Tables and Views, you must create the Table and View definitions, and then either load or import the definitions into the application, depending on whether they are internal or external to the module. The following sections outline this process for Tables and Views that are both internal and external to the module.

Import Junos PyEZ’s Predefined Tables and Views

The Junos PyEZ jnpr.junos.op module and jnpr.junos.command module provide predefined Table and View definitions for some common operational RPCs and commands. To use Junos PyEZ’s predefined Tables and Views in your Python application, you must include the appropriate import statements in your application. Along with importing the Junos PyEZ Device class, you must also import any required Tables.

The following example imports a predefined operational Table, EthPortTable, from the jnpr.junos.op.ethport module:

After you import the Table and View definitions, you can use them as described in Use Tables and Views. The following example retrieves the data for the RPC defined in the Table and then prints the interface name and operational status.

For more information about Junos PyEZ’s predefined Tables and Views, see Predefined Junos PyEZ Operational Tables (Structured Output).

Load Inline Tables and Views

To create, load, and use custom inline Tables and Views in your Junos PyEZ application:

  1. Import the following classes and libraries in your module:
  2. Define one or more Tables and Views in YAML as a multiline string.
  3. Load the Table and View definitions by including the following statement, where string-name is the identifier for the multiline string that contains the Table/View definition:
  4. Connect to the device and use the Table to retrieve information, configure the device, or both, depending on the type of Table, for example:

After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module. For additional information, see Use Tables and Views.

Import External Tables and Views

External Table and View definitions are placed in files that are external to your Junos PyEZ application. To create external custom Tables and Views and import them into your Junos PyEZ application:

  1. Define one or more Tables and Views in YAML, and save them to a file that has a .yml extension.
  2. Create a file that has the same base name as your Table file but uses a .py extension, and include the following four lines of code in the file.
  3. If the .yml and .py files are located in a subdirectory, include an __init__.py file in that subdirectory, so that Python checks this directory when it processes the Table import statements in your application.
  4. In the Junos PyEZ application, import the Device class and any required Tables.
  5. Connect to the device and use the Table to retrieve information, configure the device, or both, depending on the type of Table, for example:

After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module. For additional information, see Use Tables and Views.

Use Tables and Views

After you load or import the Table and View definitions, you can use the predefined, custom inline, or custom external Tables in the same manner.

To use a Table:

  1. Create the Device instance and open a connection to the target device:
  2. Create the Table instance and associate it with the Device instance.
  3. Use the Table to retrieve information, configure the device, or both, depending on the type of Table.
  4. Iterate over and manipulate the resulting object to extract the required information.

The following example imports a custom external Table, UserTable. The application connects to the device and calls the Tables’s get() method to retrieve user objects from the [edit system login] hierarchy level. The application then prints each username and its corresponding login class.

For more information about using Junos PyEZ Tables, see the following topics: