Use Salt with Junos PyEZ Tables to Retrieve Operational and Configuration Data from Devices Running Junos OS
Juniper Networks provides support for using Salt to manage devices running Junos OS, and the
Junos execution and state modules (for Salt) define functions that enable you to perform
operational and configuration tasks on the managed devices. The salt.modules.junos.get_table execution function and the
salt.states.junos.get_table state function can reference
predefined or custom Junos PyEZ Tables to extract operational information or
configuration data from a device running Junos OS.
Understanding Junos PyEZ Tables
Junos PyEZ is a microframework for Python that enables you to manage and automate devices running Junos OS. The Junos proxy minion (for Salt) connects to the Salt master using the ZeroMQ event bus and uses the Junos PyEZ library to connect to the device running Junos OS. As a result, Salt can leverage Junos PyEZ features when managing the devices.
Junos PyEZ supports using simple YAML definitions, which are
referred to as Tables and Views, to retrieve and filter operational command output and configuration
data from devices running Junos OS. Junos PyEZ operational (op) Tables
extract information from the output of operational commands or RPCs,
and configuration Tables retrieve specific configuration data. The
Junos PyEZ jnpr.junos.op modules contain
predefined Table and View definitions for some common RPCs. You can
also create custom Tables and Views.
When you use Salt to manage devices running Junos OS, the junos.get_table function can use Junos PyEZ Tables
to retrieve data from a device. The function can reference the predefined
operational Tables and Views that are included with the Junos PyEZ
distribution, or it can reference user-defined operational and configuration
Tables and Views that reside on either the Salt master or a separate
proxy minion server.
For general information about Junos PyEZ Tables and Views, see the following sections and related documentation in the Junos PyEZ Developer Guide:
junos.get_table Function Overview
The junos.get_table function can
use Junos PyEZ Tables to retrieve data from a device running Junos
OS. The basic syntax for the junos.get_table execution function and state function is presented here. For a complete
list of the available function parameters, see the API documentation
for the function.
The salt.modules.junos.get_table execution function syntax
is:
salt 'target' junos.get_table table table_file path
The salt.states.junos.get_table state function syntax
is:
id:
junos.get_table:
- table: table
- table_file: table_file
- path: path
where:
id—User-defined identifier for the state declaration.
path: path—(Optional) Path to the YAML file that contains the Table and View definitions.table: table—Name of the Junos PyEZ Table.table_file: table_file—Filename of the YAML file that contains the Table and View definitions.
You use the path and table_file parameters to specify the path and filename
of the YAML file containing the Table definition. If you import a
Table from the jnpr.junos.op module included
with the Junos PyEZ distribution, you can omit the path parameter. In this case, the get_table function automatically checks the appropriate directory in the Junos
PyEZ install path for the specified Table file. You can also specify
a path to a custom Table. If you use a custom Table, the Table can
reside on the Salt master or proxy minion server. Table 1 outlines the
different Tables you can use, the location of the Tables, and the
required value for the path parameter.
Table |
Location |
|
Example |
|---|---|---|---|
jnpr.junos.op module Tables |
jnpr/junos/op directory under the Junos PyEZ install path on the proxy minion server |
– |
junos.get_table: - table: ArpTable - table_file: arp.yml |
Custom Tables |
Salt master |
Path (using salt:// notation) to the YAML file containing the custom Table. |
junos.get_table: - table: IFTable - table_file: IFTable.yml - path: salt://tables |
Proxy minion server |
Absolute path to the YAML file containing the custom Table. |
junos.get_table: - table: IFTable - table_file: IFTable.yml - path: /srv/salt/tables |
How to Use the junos.get_table Function
You can call the junos.get_table function
to use a Junos PyEZ Table to retrieve operational or configuration
data from a device running Junos OS. The following command uses the ArpTable Table in the jnpr.junos.op module to retrieve Address Resolution Protocol (ARP) entries for
the target device. The Table is defined in the arp.yml file located in the Junos PyEZ jnpr/junos/op directory.
saltuser@salt-master:~$ sudo salt 'router1' junos.get_table ArpTable arp.yml
The function’s response includes the device’s hostname,
the Table and View, and the out key, which
returns True if the operation is successful.
If the operation is successful, the response also includes the reply key, which contains the Table items. Each Table
item includes the fields defined by the View and the value extracted
from the data for each of the corresponding fields.
router1:
----------
hostname:
router1.example.com
out:
True
reply:
----------
00:50:56:93:7c:13:
----------
interface_name:
fxp0.0
ip_address:
198.51.100.10
mac_address:
00:50:56:93:7c:13
f8:c0:01:18:8b:67:
----------
interface_name:
fxp0.0
ip_address:
198.51.100.254
mac_address:
f8:c0:01:18:8b:67
...
table:
----------
ArpTable:
----------
args:
----------
no-resolve:
True
item:
arp-table-entry
key:
mac-address
rpc:
get-arp-table-information
view:
ArpView
ArpView:
----------
fields:
----------
interface_name:
interface-name
ip_address:
ip-address
mac_address:
mac-address
tablename:
ArpTable
As another example, the following state file retrieves interface status information using a custom Junos PyEZ Table named IFTable. The Table and View are defined in the /srv/salt/tables/IFTable.yml file on the Salt master.
saltuser@salt-master:~$ cat /srv/salt/junos_get_table_interface_status.sls
Get Interface Status Information:
junos.get_table:
- table: IFTable
- table_file: IFTable.yml
- path: salt://tables
saltuser@salt-master:~$ sudo salt 'router1' state.apply junos_get_table_interface_status
router1:
----------
ID: Get Interface Status Information
Function: junos.get_table
Result: True
Comment:
Started: 22:51:38.947322
Duration: 373.053 ms
Changes:
----------
hostname:
router1.example.com
out:
True
reply:
----------
cbp0:
----------
adstat:
up
name:
cbp0
opstat:
up
...
How to Define Optional Parameters in Tables and Views
Junos PyEZ Tables and Views can include a number of optional parameters depending on the type of Table. The Table and View definitions can define default values for these parameters. You can also specify or override the value for one or more parameters when you use the Table in an application.
The junos.get_table function enables you to define a value or override the
default value for many of these same parameters. For example, when you call the
junos.get_table function, you can include the
key argument for Tables that support the corresponding
parameter. You can also include the table_args argument to
supply key/value pairs for the args parameter in op Tables. For
information about the parameters supported by the different types of Tables and
Views, see the Junos PyEZ Developer Guide.
Consider the EthPortTable Table in
the jnpr.junos.op module, which executes
the get-interface-information RPC. The RPC uses the default
arguments media: True and interface_name: '[afgxe][et]-*'.
---
EthPortTable:
rpc: get-interface-information
args:
media: True
interface_name: '[afgxe][et]-*'
args_key: interface_name
item: physical-interface
view: EthPortViewIn this case, when you call the junos.get_table function, you can include optional parameters that are supported
by op Tables that execute RPCs and return structured output. For example,
you can include the table_args parameter
to override the default values for the RPC arguments that are defined
in args.
The following command uses EthPortTable to retrieve interface information from the device, and it includes
the table_args parameter to only return
data for 10-Gigabit Ethernet (GbE) interfaces instead of the default
set of interfaces defined in the Table. The Table definition displayed
in the output reflects the updated arguments under args.
saltuser@salt-master:~$ sudo salt 'router1' junos.get_table EthPortTable ethport.yml table_args='{"interface_name" : "xe-*"}'
router1:
----------
...
table:
----------
EthPortTable:
----------
args:
----------
interface_name:
xe-*
media:
True
args_key:
interface_name
item:
physical-interface
key:
name
rpc:
get-interface-information
view:
EthPortView
...The equivalent state file is:
saltuser@salt-master:~$ cat /srv/salt/junos_get_table_ethport.sls
Retrieve Ethernet port information:
junos.get_table:
- table: EthPortTable
- table_file: ethport.yml
- table_args:
interface_name: 'xe-*'