cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1538
Views
5
Helpful
4
Replies

How to use device template and apply it from python code ?

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,

1 Accepted Solution

Accepted Solutions

snovello
Cisco Employee
Cisco Employee
There is generic documentation on how to call any action from python.
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

View solution in original post

4 Replies 4

snovello
Cisco Employee
Cisco Employee
There is generic documentation on how to call any action from python.
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

Thank you very much for your help Sir.

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}')

 

jvikman
Cisco Employee
Cisco Employee

Do you get any help from the ncs-python-vm log files? Check both the vm and the package specific logs.