- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 03:15 AM
Hello,
I'm trying to use device templates and applying it to a device from a python Action. How to reproduce the following command using python:
devices device DEV-NAME apply-template template-name TEMP-NAME variable { name VAR-NAME1 value VALUE1 }
variable { name VAR-NAME2 value VALUE2 }
I didn't find any documentation about apply_template action.
Best regards,
Solved! Go to Solution.
- Labels:
-
Developer
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 05:47 AM
See here:
https://developer.cisco.com/docs/nso/guides/#!python-api-overview/maagic-examples
For applying a template, you have to be in a transaction, and then apply the transaction just like the example, inside that:
In [2]: app = x.devices.device["ce0"].apply_template
In [3]: inp = app.get_input()
In [4]: inp.template_name = "MY-TEMPLATE"
In [5]: name1 = inp.variable.create('NAME1')
In [6]: name1.value = "VALUE1"
In [7]: result = app(inp)
I suggest using ipython with NSO, so you get tab completion and you can explore what is available in python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 05:47 AM
See here:
https://developer.cisco.com/docs/nso/guides/#!python-api-overview/maagic-examples
For applying a template, you have to be in a transaction, and then apply the transaction just like the example, inside that:
In [2]: app = x.devices.device["ce0"].apply_template
In [3]: inp = app.get_input()
In [4]: inp.template_name = "MY-TEMPLATE"
In [5]: name1 = inp.variable.create('NAME1')
In [6]: name1.value = "VALUE1"
In [7]: result = app(inp)
I suggest using ipython with NSO, so you get tab completion and you can explore what is available in python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 01:11 PM
Thank you very much for your help Sir.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2023 09:04 PM
Old thread but I just hit the same problem. NSO6.1.4 and the same code does not work. It shows transaction being processed in devel.log however nothing changes in the CDB in config for the device. My setup uses local NSO instance which has load-merged config from real device but device is never sync-ed. Commit is done with no-networking parameter. What am I doing wrong here ?
try:
with ncs.maapi.single_write_trans(uinfo.username, uinfo.context, ["ncsadmin"]) as t:
root = ncs.maagic.get_root(t)
ztp_config_cdb = ncs.maagic.get_node(t, kp)
app = root.devices.device[ztp_config_cdb.name].apply_template
inp = app.get_input()
inp.template_name = 'day1-smf'
var1 = inp.variable.create('profile_name')
var1.value = '\'JUN2023\''
app(inp)
t.apply(flags=16)
except _ncs.error.Error as e:
self.log.error('Error while applying template on the configuration.')
self.log.error(f'Error: {e}')

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2023 10:30 PM
Do you get any help from the ncs-python-vm log files? Check both the vm and the package specific logs.
