Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Junos PyEZ Configuration Tables to Retrieve Configuration Data

Junos PyEZ configuration Tables and Views provide a simple and efficient way to extract specific information from the selected configuration database of a Junos device. After loading or importing the Table definition into your Python module, you can retrieve the configuration data.

Junos PyEZ configuration Tables that specify the get property can only retrieve configuration data. Tables that specify the set property can configure resources on Junos devices as well as retrieve data in the same manner as Tables that specify the get property.

To retrieve information from a specific device, you must create a Table instance and associate it with the Device object representing the target device. For example:

The following sections discuss how to then retrieve and manipulate the data:

Retrieve Configuration Items

The configuration Table get or set property identifies the data to extract from the configuration. For example, the following sample Table definition extracts user elements at the [edit system login] configuration hierarchy level:

You retrieve the configuration data in your Python script by calling the get() method and supplying any desired arguments.

If the Table definition includes the required_keys parameter, you must include key-value pairs for each required key in the get() method argument list. The following Table definition requires that the get() method argument list include a user argument with a value that corresponds to the value of a name element at the [edit system login user] hierarchy level:

In the get() method, you must include the required key in the argument list; otherwise, the code throws a ValueError exception. The following example requests the configuration data for the user named 'operator':

Note:

If the argument name is hyphenated, you must change any dashes in the name to underscores. The argument value, however, is a string and as such can contain hyphens.

You can include the get() method namesonly=True argument to return configuration data that contains only name keys at the hierarchy level specified in the get or set property of your Table definition.

For example, if get is defined to retrieve configuration data at the interfaces/interface hierarchy level, and you include the namesonly=True argument in the get() method when you retrieve the data in your Junos PyEZ script, the method only returns the values in the <name> elements that are direct children of the interfaces/interface hierarchy level. Information in elements that are siblings of the <name> element is not returned, and data for <name> elements at lower levels in the hierarchy is not returned.

Specify the Configuration Database

By default, Junos PyEZ configuration Tables retrieve data from the candidate configuration database. When you call the get() method in the Python script to retrieve the Table data, you can specify that the method should instead return data from the committed configuration database by passing in the options argument and including the 'database':'committed' item in the options dictionary.

Specify Inheritance and Group Options

You can control inheritance and group options when you retrieve configuration data by using the options argument in the get() method argument list. The options argument takes a dictionary, and by default is set to the following value, which applies inheritance and groups for the returned configuration data:

If you do not redefine the options argument in your Python script, it automatically uses the default.

The inherit option specifies how the configuration data displays statements that are defined in configuration groups and interface ranges. By default, the 'inherit':'inherit' option is included, and the configuration data encloses tag elements inherited from user-defined groups or interface ranges within the inheriting tag elements rather than display the <groups>, <apply-groups>, <apply-groups-except>, or <interface-range> elements separately. To apply inheritance but also include tag elements for statements defined in the junos-defaults group, use 'inherit':'defaults' in the options argument.

To disable inheritance, set the dictionary value to an empty string.

Including both the 'inherit':'inherit' and 'groups':'groups' options returns configuration data that also indicates the configuration group from which elements are inherited. An element that is inherited from a particular group includes the junos:group="source-group" attribute in its opening tag, as shown in the following example:

To provide access to the attributes in the View definition, you can include the appropriate XPath syntax in the field mapping. The following example defines the ifgroup field and maps it to the junos:group attribute of the interface’s <name> element:

Junos PyEZ also provides the group operator, which is a shortcut method for accessing the junos:group attribute of an element. The following example defines the ifgroup field, which is mapped to the name element with the group operator. When you access ifgroup within your script, it references the value for the junos:group attribute associated with the interface’s <name> element.

If an element is not inherited from a group, the value of a field that references the group attribute is None.

Access Table Items

After you retrieve the configuration items, you can treat them like a Python dictionary, which enables you to use methods in the standard Python library to access and manipulate the items.

To view the list of dictionary keys corresponding to the configuration item names, call the keys() method.

You can verify that a specific key is present in the Table items by using the Python in operator.

To view a list of the fields, or values, associated with each key, call the values() method. The values() method returns a list of tuples with the name-value pairs for each field that was defined in the View.

To view the complete list of items, including both keys and values, call the items() method.

Iterate Through a Table

Tables support iteration, which enables you to loop through each configuration item in the same way that you would loop through a list or dictionary. This makes it easy to quickly format and print desired fields.

The following Table definition extracts the system/login/user items from the configuration data:

The following Junos PyEZ application loops through the user items and prints the name and class of each user:

The username and userclass fields, which are defined in UserView, correspond to the values of the name and class elements, respectively, in the configuration data. The output includes the user’s name and class.

Although UserView defines a username field that maps to the name element, by default, each View item has a name property that references the key that uniquely identifies that item. Thus, you could also use user.name in this example to reference the value of the name element.