Automation

Scripting How-To: Read the configuration out of a Junos device using Tables/Views

By Elevate posted 08-07-2015 16:10

  

Read the Configuration Out of a Junos Device Using Tables and Views

 

Using the SRX examples in GitHub, https://github.com/Juniper/py-junos-.../cfgro/srx.yml, the Table uses get to retrieve a configuration stanza. A VRF query table was created below that is in a position to retrieve all the VRF instance names, and succesfully load all VRF instance names.

 

VRF Query Table/View VRF:

 

1 get: routing-instances/instance args_key: name view: VRFView VRFView: fields: instance_name: name

 

To retrieve entire sections of the configuration and group per instance, the PyEZ techwiki specifies that configuration can be retrieved using Table/View. 
 

To Retrieve Using Table/View:


Yaml file content (change according to your need): VRF:
get: routing-instances/instance
args_key: name
view: VRFView

VRFView:
fields:
instance_name: name
instance_type: instance-type
rd_type: route-distinguisher/rd-type
vrf_import: vrf-import
vrf_export: vrf-export


Code:
tbl = VRF(dev)
#The change is below set the value=True. When Values set to true allows the retrieval of all data not just named-keys
tbl.get(values=True)
#make sure to pass values=True
for item in tbl:
print 'instance_name:', item.instance_name
print 'instance_type:', item.instance_type
print 'rd_type:', item.rd_type
print 'vrf_import:', item.vrf_import
print 'vrf_export:', item.vrf_export

The previous code was meant for the xml below (output of "show routing-instances | display xml"). Change the YAML file ccording to your need:


<routing-instances>
<instance>
<name>blue-vr</name>
<instance-type>vrf</instance-type>
<interface>
<name>fe-0/0/2.0</name>
</interface>
<route-distinguisher>
<rd-type>10.58.255.1:37</rd-type>
</route-distinguisher>
<vrf-import>test-policy</vrf-import>
<vrf-export>test-policy</vrf-export>
</instance>
<instance>
<name>red-vr</name>
<instance-type>vrf</instance-type>
<interface>
<name>fe-0/0/3.0</name>
</interface>
<route-distinguisher>
<rd-type>10.58.255.1:38</rd-type>
</route-distinguisher>
<vrf-import>test-policy</vrf-import>
<vrf-export>test-policy</vrf-export>
</instance>
</routing-instances>

#yaml
#junospyez
#ScriptingHow-To
#How-To
1 comment
0 views

Permalink

Comments

08-23-2014 18:03

sotiris.ioannou@interoute.com says:
 
The answer was provided by Nitin Kumar on the PyEZ Google Group:

Please try out this way:

Yaml file content (change according to your need):
VRF:
get: routing-instances/instance
args_key: name
view: VRFView

VRFView:
fields:
instance_name: name
instance_type: instance-type
rd_type: route-distinguisher/rd-type
vrf_import: vrf-import
vrf_export: vrf-export


Code:
tbl = VRF(dev)
#The change is below set the value=True. When Values set to true allows the retrieval of all data not just named-keys
tbl.get(values=True)
#make sure to pass values=True
for item in tbl:
print 'instance_name:', item.instance_name
print 'instance_type:', item.instance_type
print 'rd_type:', item.rd_type
print 'vrf_import:', item.vrf_import
print 'vrf_export:', item.vrf_export

Above code was meant for below xml(output of "show routing-instances | display xml"), hence according to your need do change the YAML file:
<routing-instances>
<instance>
<name>blue-vr</name>
<instance-type>vrf</instance-type>
<interface>
<name>fe-0/0/2.0</name>
</interface>
<route-distinguisher>
<rd-type>10.58.255.1:37</rd-type>
</route-distinguisher>
<vrf-import>test-policy</vrf-import>
<vrf-export>test-policy</vrf-export>
</instance>
<instance>
<name>red-vr</name>
<instance-type>vrf</instance-type>
<interface>
<name>fe-0/0/3.0</name>
</interface>
<route-distinguisher>
<rd-type>10.58.255.1:38</rd-type>
</route-distinguisher>
<vrf-import>test-policy</vrf-import>
<vrf-export>test-policy</vrf-export>
</instance>
</routing-instances>

Thanks
Sotiris
Posted 06:03, 23 Aug 2014