Showing results for 
Search instead for 
Did you mean: 

Create template via Maapi

I'm trying to create and apply a template in NSO 4.7 directly through the Python code, using Maapi and I'm not getting it. Is there any way to do this?
I found an example that it's possible to change the devices via Maapi but I can't create any template directly by it.

Cisco Employee

Re: Create template via Maapi

What problem are u having?


Re: Create template via Maapi

I'm trying to instantiate a service through Maapi.

I am using this code.


from ncs import maapi
from ncs.application import Service
from ncs.dp import Action

import _ncs.error

class ApplyTemplate(Action):
def cb_action(self, uinfo, name, kp, input, output):'invoke action %s with template %s' % (name,

def apply_template(t, i):
template = ncs.template.Template(t, i.context_node)
vars = ncs.template.Variables()
for v in i.variable:
vars.add(, v.value)
template.apply(, vars)

do_apply = False
m = maapi.Maapi()
if uinfo.actx_thandle != -1:
# When invoked from the CLI we get a transaction
# Note: unless we are in configure mode it will be read-only
m.attach2(0, 0, uinfo.actx_thandle)
trans = maapi.Transaction(m, uinfo.actx_thandle)
# Start write transaction and apply it at end
trans = m.start_write_trans(usid=uinfo.usid)
do_apply = True

apply_template(trans, input)
except _ncs.error.Error as e:
if e.confd_errno == ncs.ERR_NOT_WRITABLE:
# Happens when invoked from the CLI and not in configure mode,
# assume user wants us to start a separate transaction and
# apply the template there.
trans = m.start_write_trans(usid=uinfo.usid)
do_apply = True
apply_template(trans, input)
raise e

if do_apply:


I found this project on Github.



The code runs without error however when I enter the CLI no service has been created.

Cisco Employee

Re: Create template via Maapi

Firstly, what is the template you are trying to apply? Also if you main goal is to create an NSO service you can do that directly without involving an action to do so.


Re: Create template via Maapi

I'm trying to apply the l2vpn template.
I should do it straight into NSO but I need to do it pragmatically.
Cisco Employee

Re: Create template via Maapi

Likewise, I am a bit confused on what you are attempting to do.

Generally, templates are generated from the CDB config schema (device config, service instance config, etc.) that you want to parameterize and applied via the template.apply mechanism.  Applying the template sets the CDB config specified in the template in you transaction.  Applying the transaction, then commits those changes.

Although it seems feasible via some python code, I've not seen a case where the config template is generated 'on-the-fly' and then applied in an action execution, and certainly haven't seen any maapi methods for that specifically.

In your code below - '' will be the name the template (''.xml filename) expected to be found in your packages/<service>/template directory:

template.apply(, vars)

Here's an example of applying a template in a transaction:


    def cb_action(self, uinfo, name, kp, input, output):'action name: ', name)


        # open read/write transaction

        with ncs.maapi.single_write_trans('admin', 'UP') as t:

              vars = ncs.template.Variables()

              vars.add('DEVICE', input.device)

              vars.add('HNAME', input.hname)

              vars.add('DNAME', input.dname)


              # when calling a template it needs a context node, normally, for a service

              # this is the service node. Here we want context to point to devices tree...

              root = ncs.maagic.get_root(t)

              context_node = root.ncs__devices

              # create a Template object, passing in the context node

              tmpl = ncs.template.Template(context_node)

              # apply the template.

              tmpl.apply('tempBar-template', vars)


              # commit transaction including template config changes



$ cat tempBar-template.xml

<config-template xmlns="">

  <devices xmlns="">




          <!--  juniper stuff -->

          <configuration xmlns="">






          <!--  juniper stuff -->