cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3201
Views
1
Helpful
4
Replies

How to write NSO-action (python) to get its dry-run output?

viskakka@cisco.com
Cisco Employee
Cisco Employee

Got a requirement to create a NSO action wherein customer want ability to run action to see its dry-run output, before actually committing the action. Is it possible? Any example would be great help.

1 Accepted Solution

Accepted Solutions

lmanor
Cisco Employee
Cisco Employee

Dry-run is a service action, here is an example of how it can be run, this does native device output, can also used cli and xml - see tailf-ncs-services.yang for details:

              # Run dry-run before committing

              dryRun = root.services.commit_dry_run

              drInput = dryRun.get_input()

              drInput.outformat = 'native'

              self.log.info('tryAction: Dry-run outformat : ', drInput.outformat)

              drOutput = dryRun(drInput)

              if drOutput.native != None :

                devlist = drOutput.native.device

                for dev in devlist :

                  self.log.info('tryAction: Dry-run device: ', dev.name)

                  self.log.info('tryAction: Dry-run data: ', dev.data)

-Larry

View solution in original post

4 Replies 4

lmanor
Cisco Employee
Cisco Employee

Dry-run is a service action, here is an example of how it can be run, this does native device output, can also used cli and xml - see tailf-ncs-services.yang for details:

              # Run dry-run before committing

              dryRun = root.services.commit_dry_run

              drInput = dryRun.get_input()

              drInput.outformat = 'native'

              self.log.info('tryAction: Dry-run outformat : ', drInput.outformat)

              drOutput = dryRun(drInput)

              if drOutput.native != None :

                devlist = drOutput.native.device

                for dev in devlist :

                  self.log.info('tryAction: Dry-run device: ', dev.name)

                  self.log.info('tryAction: Dry-run data: ', dev.data)

-Larry

Hi Larry,

Thanks for sharing above way to output dry run output. But i am still missing something.

My action(Its not a service) is applying a template. So requirement is to run this action to produce dry-run output before actually applying changes to network.

So my action yang is taking an input argument to check if invocation is just to produce dry-run or actual commit.

based on this argument, i need to write python logic to either just produce dry-run output for user OR actual push.

Thanks

Vishal.

Hi Vishal

Larry gave you the correct way to do this...

But to add a few more details, assuming you have (in part) some YANG like this:

  container action {

    tailf:action trystuff {

      tailf:actionpoint tempBar-action;


      input {

       (...skip)

        leaf doDryCommit {

          tailf:info "dryrun or commit";

          type enumeration {

            enum dry-run;

            enum commit;

          }

          default "dry-run";

        }

      } // input

then you Python will look (something) like this:

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

            try:

                vars = ncs.template.Variables()

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

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

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

                #

                                  root = ncs.maagic.get_root(t)

                context_node = root.ncs__devices   

                tmpl = ncs.template.Template(context_node)

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

                #....

                # dryrun-native

                #....

                       if str(input.doDryCommit) == 'dry-run':

                    dryRun = root.services.commit_dry_run

                    drInput = dryRun.get_input()

                    drInput.outformat = 'native'

                    drOutput = dryRun(drInput)

                    if drOutput.native != None :

                        devlist = drOutput.native.device

                        if len(devlist) != 0:

                            for dev in devlist:

                                self.log.info('tempBar: Dry-run device : ', dev.name)

                                self.log.info('tempBar: Dry-run output : ', dev.data)

                        #....

                        else:

                            self.log.info('tempBar: Dry-run device : ', dev.name)

                            self.log.info('tempBar: Dry-run output : NO Dry-Run found')

                #....

                # commit

                #....

                       else:   # commit selected

                    self.log.info('tempBar: commit template apply')

                    # commit transaction

                    t.apply()


                self.log.info('tryAction: Host ... : ', input.hname)

                output.result = "all done"


            except Exception, cause:

                res = str(cause)

                self.log.info('tryAction: Exception: %s ', res)

                output.result = "exception"


            finally:

                t.finish_trans()





hope that helps

Gregg

Thanks Gregg for detailed example.

I am able to implement it in my action, appriciate your inputs. Thanks to Larry too.