cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5446
Views
15
Helpful
13
Replies

Is there a way to get "commit dry-run" from python?

cnicasio
Level 1
Level 1

Hi, it would be very useful for a code I'm writing in Python, to be able to get "commit dry-run outfomat xml" like output in python.

The code is like

...

with ncs.maapi.single_write_trans('admin', 'python') as t:
....
several maagic writes to CDB
...

At this point, before applying the transaction, I would lke to get CLI o XML output similar to "commit dry-run..." on the NSO CLI..

I tried t.apply with COMMIT_NCS_DRY_RUN_XML flag, but didn't get any result..

Anybody knows if that is possible?

Regards

13 Replies 13

vleijon
Cisco Employee
Cisco Employee
If you use apply_params it ought to work.

Thanks a lot...I don't find documentation on that function...would be nice if you have an example

It is documented in the main python documentation, so you can do pydoc ncs.maapi.Transaction.apply_params. But, I think essentially it works the way you wanted apply to work, set flags to dry run and it will return a dictionary with dry-run results in it.

You can find apply_params() pydocs here: nso-5.2.0.1/doc/api/python/ncs.maapi.html

CommitParams class here: nso-5.2.0.1/src/ncs/pyapi/pysrc/maapi.py

 

Using CommitParams() along with apply_params() you can do dry-run like this:

...: cp.dry_run_native()
#...: cp.dry_run_cli()
#...: cp.dry_run_native()
#...: cp.dry_run_xml()
...: r = t.apply_params(True, cp)
...: print r
...:
{'device': {'ios-1': '\n\nhostname Test_75\n'}, 'outformat': 'native', 'dry-run': True}
 
 

OK! thanks a lot for your help!

Is there a way to do this in a service? I attempted to get the transaction of the service and follow this approach but I am getting:
r = t.apply_params(True, cp)
File "/root/nso/nso-versions/5.2.1/src/ncs/pyapi/ncs/tm.py", line 12, in wrapper
return fn(*args, **kwargs)
File "/root/nso/nso-versions/5.2.1/src/ncs/pyapi/ncs/maapi.py", line 1325, in apply_params
tvresult = self.maapi.apply_trans_params(self.th, keep_open, tvparams)
File "/root/nso/nso-versions/5.2.1/src/ncs/pyapi/ncs/maapi.py", line 273, in proxy
return real(self2.msock, *args, **kwargs)
Error: Bad protocol usage or unexpected retval (21): not allowed for attached transaction

Thanks!

HI Johan,

I don't recognize the this exact error, but usually it is not prudent to apply a transaction 'inside' of a transaction - tends to get into a transaction lock deadlock situation.

See this thread previous thread on a similar:

https://community.cisco.com/t5/nso-developer-hub-discussions/call-partial-sync-from-function-in-cb-create/td-p/3895272

 

Presumably, you are attempting to save off the dry-run config that is getting written to the network. I've seen this done through a 2-step action, does a transaction for the dry-run followed by a transaction for the actual service instance commit. Also seen it done via a custom cli command that issues subsequent commit-dry-run and commit commands. I'm sure a reactive fastmap service could be crafted to do the dry-run/commit sequence as well.

Hi Imanor,
As per above logic, t.apply_params(True, cp) will always commit the transaction.

We have 1 scenario where we just need to show dry-run output without doing actual commit from an action.
So is it possible to do so ?
We are aware of the old way by using services commit-dry-run like below
```

dryRun = root.ncs__services.commit_dry_run
dryRunInp = dryRun.get_input()
dryRunInp.outformat = 'cli'
dryRunResult = dryRun(dryRunInp)
cliDryRun = dryRunResult.cli.local_node.data
```

However since it is deprecated; we are looking for new way.

 

 

Thanks in advance

Hello, 

I am not seeing the behavior that you see, the dry-run commit is _not_ committed to the running-config:

 

[edit]
admin@ncs% show devices device ios-1 config ios:hostname
ios:hostname TEST1;
[ok][2020-11-04 11:13:30]

 

In [8]: with ncs.maapi.Maapi() as m:
...: with ncs.maapi.Session(m, 'admin', 'system'):
...: with m.start_write_trans() as t:
...: root = ncs.maagic.get_root(t)
...: name = 'ios-1'
...: dev = root.ncs__devices.device['ios-1']
...: dev.config.ios__hostname = 'Test_82'
...: cp = ncs.maapi.CommitParams()
...: cp.dry_run_cli()
...: r = t.apply_params(True, cp)
...: print (r)
...:
{'dry-run': True, 'outformat': 'cli', 'local-node': ' devices {\n device ios-1 {\n config {\n- ios:hostname TEST1;\n+ ios:hostname Test_82;\n }\n }\n }\n'}

 

[edit]
admin@ncs% show devices device ios-1 config ios:hostname
ios:hostname TEST1;
[ok][2020-11-04 11:14:04]

 

In [9]: with ncs.maapi.Maapi() as m:
...: with ncs.maapi.Session(m, 'admin', 'system'):
...: with m.start_write_trans() as t:
...: root = ncs.maagic.get_root(t)
...: name = 'ios-1'
...: dev = root.ncs__devices.device['ios-1']
...: dev.config.ios__hostname = 'Test_82'
...: cp = ncs.maapi.CommitParams()
...: cp.dry_run_xml()
...: r = t.apply_params(True, cp)
...: print (r)
...:
{'dry-run': True, 'outformat': 'xml', 'local-node': '<devices xmlns="http://tail-f.com/ns/ncs">\n <device>\n <name>ios-1</name>\n <config>\n <hostname xmlns="urn:ios">Test_82</hostname>\n </config>\n </device>\n</devices>\n'}

 

[edit]
admin@ncs% show devices device ios-1 config ios:hostname
ios:hostname TEST1;
[ok][2020-11-04 11:14:44]

 

In [10]: with ncs.maapi.Maapi() as m:
...: with ncs.maapi.Session(m, 'admin', 'system'):
...: with m.start_write_trans() as t:
...: root = ncs.maagic.get_root(t)
...: name = 'ios-1'
...: dev = root.ncs__devices.device['ios-1']
...: dev.config.ios__hostname = 'Test_82'
...: cp = ncs.maapi.CommitParams()
...: cp.dry_run_native()
...: r = t.apply_params(True, cp)
...: print (r)
{'dry-run': True, 'outformat': 'native', 'device': {'ios-1': '\n\nhostname Test_82\n'}}

 

[edit]
admin@ncs% show devices device ios-1 config ios:hostname
ios:hostname TEST1;
[ok][2020-11-04 11:16:49]

 

I have the same problem. NSO Version 5.6.4

mohamkh7
Cisco Employee
Cisco Employee

Got it Thanks. 

Hi all,

I am looking to access the Commit Parameters (dry-run, no-networking, etc) in the cb_create or in the service pre_modification.

I simply tried this :

    @service.pre_modification
    def cb_pre_modification(selftctxopkprootproplist
        with ncs.maagic.get_trans(root) as t :
            cp = ncs.maapi.CommitParams()
            self.log.info("commit parameters : {}".format(cp))
 
But it does not really work -
I don't want to modify the commit parameters but I 'd like to do a test in python later in the code if the transaction is made with 'no-networking' option. 
Any idea on how to do that ?
 
Benoit
 

Hi,

just to follow-up .

The answer is there for NSO >5.5 : Solved: Re: How to check if it is a dry-run in python for a service - Cisco Community

 

Benoit

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: