# NSO API using Python

Load **blank.ssh.enabled.cfg**

```
#IOS-XE
config replace flash:blank.ssh.enabled.cfg
 
#IOS-XR
configure
load bootflash:blank.ssh.enabled.cfg
commit replace
```

Create a python script that interacts with NSO’s northbound API.

* The script should create Lo303 on R3 with ip address 30.30.30.30/32
* After creating that loopback, the script should interate through all XE devices and print out each Loopback with its name and IP address.

## Answer <a href="#ed57c6aa-1c45-46a7-bf1e-314253118c94" id="ed57c6aa-1c45-46a7-bf1e-314253118c94"></a>

Create the following python script on the nso server.

```
import ncs

with ncs.maapi.single_write_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    device_cbd = root.devices.device['r3']
    device_cbd.config.ios__interface['Loopback'].create('300')
    device_cbd.config.ios__interface.Loopback['300'].ip.address.primary.address = '3.3.3.3'
    device_cbd.config.ios__interface.Loopback['300'].ip.address.primary.mask = '255.255.255.255'
    t.apply()


with ncs.maapi.single_read_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    devices = root.devices
    for device in devices.device:
        if 'ios-cli' in device.device_type.cli.ned_id:
            for interface in device.config.ios__interface.Loopback:
                print(f'{device.name} has loopback {interface.name} with IP {interface.ip.address.primary.address}')
```

<div align="left"><figure><img src="/files/7PQtC79juuyxY8tJdKoM" alt=""><figcaption></figcaption></figure></div>

## Explanation <a href="#id-1cb8dde3-d4d1-808c-8c36-f857515aaeee" id="id-1cb8dde3-d4d1-808c-8c36-f857515aaeee"></a>

First, we need to understand how the loopback interface is accessed in modeled config. This is different than the native IOS-XE config. We have two ways we can see this. One is using “ | display xpath” from NSO:

<div align="left"><figure><img src="/files/zP8NgeFJdsgQKsw95UDL" alt=""><figcaption></figcaption></figure></div>

Another is by staging the config and displaying in XML before aborting.

<div align="left"><figure><img src="/files/zbHiyWACwJnKOhgbVmmZ" alt=""><figcaption></figcaption></figure></div>

This gives us the modeled data for a loopback interface.

First the script starts with the following line, which means to do a single write transaction. The next line gets us to the root of the NCS config database. When opening a transaction, you must at minimum specify the user (’admin’) and the context (’python’, as opposed to CLI, web UI etc). This just allows NSO to audit where the change came from.

```
import ncs

with ncs.maapi.single_write_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
```

Next, we start specifically at device r3 and create the loopback first. We must use the **create()** method. We can’t just assign a new container/list-element using python logic such as **\[’Loopback’]\[’300’] = …**. Instead we need to use **create()** so that NSO can validate the new data against the YANG schema.

```
import ncs

with ncs.maapi.single_write_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    device_cbd = root.devices.device['r3']
    device_cbd.config.ios__interface['Loopback'].create('300')
```

However, note that while we need to use **.create()** to create a new list, we can use the python **del** to delete a leaf. This is because deleting information does not need to be validated against a YANG schema.

Finally, we add the attributes of the interface (the address and mask). Once the container is created using **create()**, we can add attributes using standard python logic. Using **t.apply()** applies the config (does a commit) within the transaction that we opened.

```
import ncs

with ncs.maapi.single_write_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    device_cbd = root.devices.device['r3']
    device_cbd.config.ios__interface['Loopback'].create('300')
    device_cbd.config.ios__interface.Loopback['300'].ip.address.primary.address = '3.3.3.3'
    device_cbd.config.ios__interface.Loopback['300'].ip.address.primary.mask = '255.255.255.255'
    t.apply()
```

Next, we use a single read transaction to iterate through all XE devices and print their loopback information. I use an if statement that checks the NED to determine whether this is XE or XR. We’ll see a better way to iterate over a group in the next lab.

```
with ncs.maapi.single_read_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    devices = root.devices
    for device in devices.device:
        if 'ios-cli' in device.device_type.cli.ned_id:
            for interface in device.config.ios__interface.Loopback:
                print(f'{device.name} has loopback {interface.name} with IP {interface.ip.address.primary.address}')
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ccie-sp.gitbook.io/ccie-spv5.1-labs/labs/nso/nso-api-using-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
