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:
from jnpr.junos import Device from myTables.ConfigTables import UserTable with Device(host='router.example.com') as dev: users = UserTable(dev)
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:
UserTable:
get: system/login/user
view: UserView
UserView:
fields:
username: name
userclass: class
You retrieve the configuration data in your Python script
by calling the get() method and supplying
any desired arguments.
from jnpr.junos import Device from myTables.ConfigTables import UserTable with Device(host='router.example.com') as dev: users = UserTable(dev) users.get()
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:
UserTable:
get: system/login/user
required_keys:
user: name
view: UserView
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':
users = UserTable(dev).get(user='operator')
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.
from jnpr.junos import Device from myTables.ConfigTables import InterfaceTable with Device(host='router.example.com') as dev: interfaces = InterfaceTable(dev) interfaces.get(namesonly=True)
For example, suppose 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. In this case, the
method returns only 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.
table_options = {'inherit':'inherit', 'groups':'groups', 'database':'committed'} with Device(host='router.example.com') as dev: users = UserTable(dev) users.get(options = table_options)
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:
options = {'inherit': 'inherit', 'groups': 'groups'}
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.
{'inherit':''}
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:
<configuration>
<interfaces>
<interface junos:group="re0">
<name junos:group="re0">fxp0</name>
<unit junos:group="re0">
<name junos:group="re0">0</name>
<family junos:group="re0">
<inet junos:group="re0">
<address junos:group="re0">
<name junos:group="re0">198.51.100.1/24</name>
</address>
</inet>
</family>
</unit>
</interface>
</interfaces>
...
</configuration>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:
InterfaceTable:
get: interfaces/interface
view: InterfaceView
InterfaceView:
fields:
ifname: name
ifaddress: unit/family/inet/address/name
ifgroup: name/@groupJunos 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.
InterfaceTable:
get: interfaces/interface
view: InterfaceView
InterfaceView:
fields:
ifname: name
ifaddress: unit/family/inet/address/name
ifgroup: { name : group }
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.
users = UserTable(dev).get() print (users.keys())
['user1', 'readonly']
You can verify that a specific key is present in the
Table items by using the Python in operator.
if 'readonly' in users:
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.
print (users.values())
[[('username', 'user1'), ('userclass', 'super-user')], [('username', 'readonly'), ('userclass', 'read-only')]]To view the complete list of items, including both keys
and values, call the items() method.
print (users.items())
[('user1', [('username', 'user1'), ('userclass', 'super-user')]), ('readonly', [('username', 'readonly'), ('userclass', 'read-only')])]
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:
UserTable:
get: system/login/user
view: UserView
UserView:
fields:
username: name
userclass: class
The following Junos PyEZ application loops through the user items and prints the
name and class of each user:
from jnpr.junos import Device from myTables.ConfigTables import UserTable with Device(host='router.example.com') as dev: users = UserTable(dev) users.get() for user in users: print("Username is {}\nUser class is {}".format(user.username, user.userclass))
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.
Username is user1 User class is super-user Username is readonly User class is read-only
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.