cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
889
Views
0
Helpful
3
Replies

Help with accessing root.devices data in python-and-template

emsmith
Level 1
Level 1

Hi Dev'ers,

 

I'm hoping you can help.  I have a service model that's built with python code and I'm trying to pull the address address off the lo0.0 interface of a Juniper.  My python code errors out on "commit dry-run" with the error:

 

Aborted: Python cb_create error. '{0} not in /ncs:devices/device{juniper-rtr}/config/junos:configuration/interfaces/interface{lo0}/unit{0}/family/inet/address'

I've verified that the {0} it's referencing is the address[0] seen below.  Here's a snippet of the python code that's failing, marked with ****:

                if "junos" in device_type:
                    device_config = root.ncs__devices.ncs__device[member_name].ncs__config
                    loopback = device_config.junos__configuration.interfaces.interface['lo0']
                    if loopback.unit['0'].family.inet.address:
****                    loopback_ip = loopback.unit['0'].family.inet.address[0].name
                        ip, mask = loopback_ip.split('/')
                        octets = ip.split('.')
                        isis['lo0_v4'] = service.area_id + ".0000.0000." + octets[2].zfill(4) + "." + octets[3].zfill(4) + ".00"

Here's what Postman returns for "family inet", as you can see there are multiple IPs. 

 

 

<inet xmlns="http://xml.juniper.net/xnm/1.1/xnm"  xmlns:junos="http://xml.juniper.net/xnm/1.1/xnm"  xmlns:ncs="http://tail-f.com/ns/ncs">
    <address>
        <name>1.2.3.4/32</name>
    </address>
    <address>
        <name>127.0.0.1/32</name>
    </address>
    <address>
        <name>10.255.0.3/32</name>
        <primary/>
        <preferred/>
    </address>
</inet>

 

 

These IPs are not known to the Python code, so the address(es) need to be referenced more generally.  In fact, I prefer to pull *only* the "primary" IP, but am unable to do so.  Does anyone know if this is possible?  Thanks!

1 Accepted Solution

Accepted Solutions

So, 'address' here is defined as a yang list with 'name' as the list key.

admin@ncs% show devices device j-0 config junos:configuration interfaces interface lo0 unit 0 family inet address | display xpath
/devices/device[name='j-0']/config/junos:configuration/interfaces/interface[name='lo0']/unit[name='0']/family/inet/address[name='192.168.1.1/32']
/devices/device[name='j-0']/config/junos:configuration/interfaces/interface[name='lo0']/unit[name='0']/family/inet/address[name='10.0.1.0/24']
[ok][2020-05-08 15:58:27]

In your case, using address[0] -> address[name=0] in which case it will not find that entry in the list.

Looping over the entries in the list will get the address of each entry,

Unfortunately, I am not familiar with what NSO300 presents.

View solution in original post

3 Replies 3

emsmith
Level 1
Level 1

I found that looping through them is effective.  I think that, unless there's a way to access the list by address[0] (etc), it invalidates some of Cisco's documentation in NSO300 about how to access these entries.

 

Has anyone else run into this?  Also, looking over the approach above, is this how folks are generally accessing and manipulating data before sending to the template?

So, 'address' here is defined as a yang list with 'name' as the list key.

admin@ncs% show devices device j-0 config junos:configuration interfaces interface lo0 unit 0 family inet address | display xpath
/devices/device[name='j-0']/config/junos:configuration/interfaces/interface[name='lo0']/unit[name='0']/family/inet/address[name='192.168.1.1/32']
/devices/device[name='j-0']/config/junos:configuration/interfaces/interface[name='lo0']/unit[name='0']/family/inet/address[name='10.0.1.0/24']
[ok][2020-05-08 15:58:27]

In your case, using address[0] -> address[name=0] in which case it will not find that entry in the list.

Looping over the entries in the list will get the address of each entry,

Unfortunately, I am not familiar with what NSO300 presents.

You're right, looping through them is probably the only method. Thanks for the quick and precise answer @lmanor