Use Ansible with Junos PyEZ Tables to Retrieve Operational Information from Junos Devices
SUMMARY Use Junos PyEZ Tables and Views in your Ansible playbooks to retrieve operational information from Junos devices.
Module Overview
Junos PyEZ operational (op) Tables provide a simple and efficient way to extract information from complex operational command output. Juniper Networks provides an Ansible module that enables you to leverage Junos PyEZ op Tables from within Ansible playbooks. Table 1 outlines the module.
Content Set |
Module Name |
---|---|
|
The table
module does not support using configuration Tables and
Views.
Understanding Junos PyEZ Tables
Junos PyEZ is a microframework for Python that enables you to manage and automate Junos 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 Junos devices.
Junos PyEZ operational (op) Tables extract information from the output of
operational commands or RPCs. 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 Ansible to manage Junos devices, the table
module can
use Junos PyEZ Tables to retrieve data from a device. The module can reference the
predefined operational Tables and Views that are included with the Junos PyEZ
distribution, or it can reference user-defined Tables and Views that reside on the
Ansible control node.
For general information about Junos PyEZ Tables and Views, see the following sections and related documentation in the Junos PyEZ Developer Guide:
How to Use the Juniper Networks Ansible Modules with Junos PyEZ Tables
The juniper.device.table
module can include the following
arguments to specify the Table to use:
-
file
—Filename of the YAML file that defines the Junos PyEZ Table and View. -
path
—(Optional) Path to the directory containing the YAML file with the Table and View definitions. The default file path is the location of the predefined Junos PyEZ op Tables, which reside in the Junos PyEZ install path under the jnpr/junos/op directory. -
table
—(Optional) Name of the Table that will be used to retrieve the data. This option is only required when a file contains multiple Table definitions or the file contains a single Table that does not include "Table" in its name.
For example, the following task retrieves data by using a custom table named
FPCTable
, which is defined in the
fpc.yaml file located in the playbook directory:
tasks: - name: Get FPC info juniper.device.table: file: "fpc.yaml" path: "{{ playbook_dir }}" table: "FPCTable"
The module’s response includes the resource
key, which contains
a list of items returned by the Table. Each list item is a dictionary containing
the field names defined by the View and the value extracted from the data for
each of the corresponding fields.
Consider the following predefined Table and View, ArpTable
and
ArpView
, in the arp.yml file of the
Junos PyEZ distribution. ArpTable
executes the
<get-arp-table-information>
RPC with the
<no-resolve/>
option, which is equivalent to the
show arp no-resolve
CLI command. The corresponding View
extracts the MAC address, IP address, and interface name for each
<arp-table-entry>
item in the response.
--- ArpTable: rpc: get-arp-table-information args: no-resolve: true item: arp-table-entry key: mac-address view: ArpView ArpView: fields: mac_address: mac-address ip_address: ip-address interface_name: interface-name
The following Ansible playbook executes the table
module, which
uses ArpTable
to retrieve Address Resolution Protocol (ARP)
information from Junos devices. Because ArpTable
is included
with the Junos PyEZ distribution and resides in the default directory for the
predefined Junos PyEZ op Tables, the path
module argument is
not required to specify the file location. In addition, because
ArpTable
is the only Table defined in the file and includes
”Table" in its name, the table
argument is not required to
specify the Table.
--- - name: Get ARP information hosts: dc1 connection: local gather_facts: no tasks: - name: Get ARP information using Junos PyEZ Table juniper.device.table: file: "arp.yml" register: result - name: Print response ansible.builtin.debug: var: result
The playbook output, which is truncated for brevity, includes the corresponding
fields, as defined by ArpView, for each <arp-table-entry>
item returned by the device.
PLAY [Get ARP information] **************************************************** TASK [Get ARP information using Junos PyEZ Table] ***************************** ok: [dc1a.example.net] TASK [Print response] ********************************************************* ok: [dc1a.example.net] => { "result": { "changed": false, "failed": false, "msg": "Successfully retrieved 2 items from ArpTable.", "resource": [ { "interface_name": "em0.0", "ip_address": "10.0.0.5", "mac_address": "02:01:00:00:00:05" }, { "interface_name": "fxp0.0", "ip_address": "198.51.100.10", "mac_address": "30:7c:5e:48:4b:40" } ] } }
The following Ansible playbook leverages the predefined Junos PyEZ operational
Table, OspfInterfaceTable
, to retrieve information about OSPF
interfaces on Junos devices. The ospf.yml file defines
multiple Tables and Views, so the module call includes the
table
argument to specify which Table to use.
--- - name: Get OSPF information hosts: dc1 connection: local gather_facts: no tasks: - name: Get OSPF interface information juniper.device.table: file: "ospf.yml" table: "OspfInterfaceTable" register: result - name: Print response ansible.builtin.debug: var: result
Specify RPC Arguments
Junos PyEZ operational Tables have an optional args
key that
defines the default command options and arguments for the RPC executed by that
Table. The application executes the RPC with the default options unless the user
overrides the defaults. In Junos PyEZ applications, you can override the default
options or pass additional options and arguments to the RPC when calling the
get()
method.
The juniper.device.table
module also enables you to override the
default options defined in the Table or pass additional options and arguments to
the RPC by using the kwargs
argument. The
kwargs
value is a dictionary of command options and values,
which must be supported by the RPC and the device on which the RPC is
executed.
For example, the predefined Junos PyEZ op Table EthPortTable
in
the ethport.yml file executes the
<get-interface-information>
RPC with the
media
command option. By default, the RPC returns
information for all interfaces that match the given regular expression for the
interface name.
EthPortTable: rpc: get-interface-information args: media: true interface_name: '[afgxe][et]-*' args_key: interface_name item: physical-interface view: EthPortView
The following Ansible playbook uses EthPortTable
to extract
information about the interfaces on Junos devices. The kwargs
argument includes interface_name: "ge-1/0/0"
, which overrides
the EthPortTable
default for interface_name
and instructs the module to retrieve the requested fields only for the ge-1/0/0
interface.
--- - name: Get interface information hosts: dc1 connection: local gather_facts: no tasks: - name: Get interface information for Ethernet interfaces juniper.device.table: file: "ethport.yml" kwargs: interface_name: "ge-1/0/0" register: result - name: Print response ansible.builtin.debug: var: result
For more information about the default and user-supplied command options and arguments in Junos PyEZ Tables, see Defining Junos PyEZ Operational Tables and Use Junos PyEZ Operational Tables and Views that Parse Structured Output.