08-18-2022 01:01 AM
Hi dear,
I know there is a cmd tool ncs_load can load the json format payload into CDB, but I'm looking for the API (REST or python) option that can do the same.
AFAIK, the maagic API load_config doesn't the json format.
Solved! Go to Solution.
08-20-2022 10:00 AM
Hello,
Here an example.
Command that i used to check before executing my script .
admin@ncs# show devices list | count
Count: 9 lines
admin@ncs# show running-config devices device TEST-LOAD-PYTHON
----------------------------------------------^
syntax error: element does not exist
The device TEST-LOAD-PYTHON wasn't present in CDB before.
import ncs
import _ncs
#admin@ncs# show running-config devices device TEST-IOS-XR-3.00 | de-select config | de-select ssh | display json
json_payload="""
{
"data": {
"tailf-ncs:devices": {
"device": [
{
"name": "TEST-LOAD-PYTHON",
"address": "127.0.0.1",
"port": 10025,
"authgroup": "default",
"device-type": {
"cli": {
"ned-id": "cisco-iosxr-cli-3.0:cisco-iosxr-cli-3.0"
}
},
"ned-settings": {
},
"commit-queue": {
},
"state": {
"admin-state": "unlocked"
},
"notifications": {
"received-notifications": {
}
}
}
]
}
}
}
"""
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session(m, 'admin', 'system'):
with m.start_write_trans() as trans:
flags = (_ncs.maapi.CONFIG_MERGE + _ncs.maapi.CONFIG_JSON )
#_ncs.maapi.load_config(m.msock, trans.th, flags, filename) ## Can be used using a file
_ncs.maapi.load_config_cmds(m.msock, trans.th, flags, json_payload,path="/")
trans.apply()
print("Trans applied!")
Command that i use to check after executing the script
admin@ncs# show running-config devices device TEST-LOAD-PYTHON
devices device TEST-LOAD-PYTHON
address 127.0.0.1
port 10025
authgroup default
device-type cli ned-id cisco-iosxr-cli-3.0
state admin-state unlocked
!
admin@ncs# show devices list | count
Count: 10 lines
08-20-2022 10:00 AM
Hello,
Here an example.
Command that i used to check before executing my script .
admin@ncs# show devices list | count
Count: 9 lines
admin@ncs# show running-config devices device TEST-LOAD-PYTHON
----------------------------------------------^
syntax error: element does not exist
The device TEST-LOAD-PYTHON wasn't present in CDB before.
import ncs
import _ncs
#admin@ncs# show running-config devices device TEST-IOS-XR-3.00 | de-select config | de-select ssh | display json
json_payload="""
{
"data": {
"tailf-ncs:devices": {
"device": [
{
"name": "TEST-LOAD-PYTHON",
"address": "127.0.0.1",
"port": 10025,
"authgroup": "default",
"device-type": {
"cli": {
"ned-id": "cisco-iosxr-cli-3.0:cisco-iosxr-cli-3.0"
}
},
"ned-settings": {
},
"commit-queue": {
},
"state": {
"admin-state": "unlocked"
},
"notifications": {
"received-notifications": {
}
}
}
]
}
}
}
"""
with ncs.maapi.Maapi() as m:
with ncs.maapi.Session(m, 'admin', 'system'):
with m.start_write_trans() as trans:
flags = (_ncs.maapi.CONFIG_MERGE + _ncs.maapi.CONFIG_JSON )
#_ncs.maapi.load_config(m.msock, trans.th, flags, filename) ## Can be used using a file
_ncs.maapi.load_config_cmds(m.msock, trans.th, flags, json_payload,path="/")
trans.apply()
print("Trans applied!")
Command that i use to check after executing the script
admin@ncs# show running-config devices device TEST-LOAD-PYTHON
devices device TEST-LOAD-PYTHON
address 127.0.0.1
port 10025
authgroup default
device-type cli ned-id cisco-iosxr-cli-3.0
state admin-state unlocked
!
admin@ncs# show devices list | count
Count: 10 lines
08-22-2022 07:20 PM
Thanks so much for your helpful example!
Actually, I didn't find "CONFIG_JSON" as the acceptable flag for "load_config” in the document, but indeed it works well!
08-23-2022 04:45 AM
Then only way to check is to test . You can report that to the BU or open an SR for that.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide