# NSO API using Python #2

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
u
```



## Task 1. Sync-from script <a href="#id-1cb8dde3-d4d1-803c-91e8-fc7863b0dce8" id="id-1cb8dde3-d4d1-803c-91e8-fc7863b0dce8"></a>

Create a python script that syncs all XE devices.

* The script should iterate over the XE group, and do a “sync-from” for each device



You can make a change to R1 and R2 to confirm that it works properly.



<details>

<summary>Task 1 Answer</summary>

```
# Sync-from all devices in NCS manually
ncs_cli -Cu admin
devices device-group XE sync-from

# Make a change to R1 and R2 directly
int Lo12
 ip address 12.12.12.12 255.255.255.255
```



```
# Use the "sync_from()" method to sync from the device
# Use the devices.device_group[name].device_name list to
# get a list of devices in that group

import ncs

with ncs.maapi.single_read_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    device_group = root.devices.device_group['XE']
    for device in device_group.device_name:
        device_object = root.devices.device[device]
        print(f'syncing from {device_object.name}')
        device_object.sync_from()
```



Check that the change was successfully synced:

<div align="left"><figure><img src="https://3072390383-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkUz2C3GqnZcWhoVL6jfk%2Fuploads%2FDqVYY1bOS8w8WHZGP29m%2Fimage.png?alt=media&#x26;token=65e33494-9834-4688-b913-da168fe3e997" alt=""><figcaption></figcaption></figure></div>



#### A note about iterating through the device group <a href="#id-9592bf53-8296-4765-a566-b952dc633826" id="id-9592bf53-8296-4765-a566-b952dc633826"></a>

All NCS config is modeled via XML, which means it can be accessed hierarchically from the python API. You simply replace the hyphens with an underscore.



For example, notice that a device group contains a leaf-list of device-names:

<div align="left"><figure><img src="https://3072390383-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkUz2C3GqnZcWhoVL6jfk%2Fuploads%2FXgiHJzezCODqxgIndZx2%2Fimage.png?alt=media&#x26;token=63592b59-6b70-4a8b-bd80-48607967a9ad" alt=""><figcaption></figcaption></figure></div>



This becomes devices.device\_group\[’XE’].device\_name in python. You can then grab each device name and index that device name via root.devices.device\[device\_name]:

```
with ncs.maapi.single_read_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    device_group = root.devices.device_group['XE']
    for device in device_group.device_name:
        device_object = root.devices.device[device]
```

</details>





## Task 2. Delete loopback script <a href="#b71c9079-0e8d-4074-adf3-13628c77ac82" id="b71c9079-0e8d-4074-adf3-13628c77ac82"></a>

Create a new script that deletes any loopback12 on all XE routers.



<details>

<summary>Task 2 Answer</summary>

```
import ncs

with ncs.maapi.single_write_trans('admin', 'python') as t:
    root = ncs.maagic.get_root(t)
    device_group = root.devices.device_group['XE']
    for device in device_group.device_name:
        device_object = root.devices.device[device]
        print(f'Deleting Loop12 for {device_object.name}')
        del device_object.config.ios__interface.Loopback['12']
    try:
        t.apply()
    except Exception as e:
        print(e)
        pass
```



Where does the “ios\_\_interface” come from? You can gather this by showing the full config in NCS and displaying as xpath. The colon (:) is replaced with double underscore (\_\_) in python:

<div align="left"><figure><img src="https://3072390383-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkUz2C3GqnZcWhoVL6jfk%2Fuploads%2F0TDiKaNpYPenLod2FJRI%2Fimage.png?alt=media&#x26;token=0c88736b-cf15-4101-a988-4e0e07f10a94" alt=""><figcaption></figcaption></figure></div>



Note that only a single transaction is supported per transaction opened using “with ncs.maapi.single\_write\_transaction()”. This means you need to iterate over all devices, and then at the end apply the transaction. You cannot apply the transaction after each device change.

</details>









