cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
681
Views
5
Helpful
3
Replies

launch commit dry-run inside python service code

Hello Team,

 

In order to log the generated config of a service after python code execution, I'm trying to launch commit dry-run action at the end of cb_create function but this can't work for me. 

My code is as follow:

 

class ServiceCallbacks(Service):

# The create() callback is invoked inside NCS FASTMAP and
# must always exist.
@service.create
def cb_create(self, tctx, root, service, proplist):
.......
.......
dryRun = rootTree.services.commit_dry_run
drInput = dryRun.get_input()
drInput.outformat = 'native'
drOutput = dryRun(drInput)
self.log.info("\n The generated Config based on the attributes is: \n")
for dev in drOutput.native.device:
self.log.info(dev.name)
self.log.info(dev.data)
template.apply('Service-template', vars)

 

drOutput is empty because the variables are not applied to the template yet.

 

Can anyone please give me a solution how can I generate the dry-run of the config ?

 

Regards,

3 Replies 3

Alexander Stevenson
Cisco Employee
Cisco Employee

 

Hello @AlaeddineJebnoun90723,

 

I found 3 discussions here on the forums which may point you in the right direction:

 

Get python action to display dry-run of values set by python (not template)
https://community.cisco.com/t5/nso-developer-hub-discussions/get-python-action-to-display-dry-run-of-values-set-by-python-not/td-p/3730969

 

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

https://community.cisco.com/t5/nso-developer-hub-discussions/how-to-write-nso-action-python-to-get-its-dry-run-output/td-p/3499887


Is there a way to get "commit dry-run" from python?
https://community.cisco.com/t5/nso-developer-hub-discussions/is-there-a-way-to-get-quot-commit-dry-run-quot-from-python/td-p/4059445

 

Hope this helps!

 

Hello @Alexander Stevenson,

 

Thank you for your reply. I have already saw these discussions but in my case I need to execute commit dry-run action from a python service and not an action. Do you have an idea how to complete this ?

 

Regards,

Nabsch
Spotlight
Spotlight

Hello,

 

Maybe i am wrong but i don't think it's possible.

 

Cb_create is the main function for FASTMAP but configuration can be pushed in premod and postmod. In your service you are applying template that need to be processed and your template can refer to another service. 

 

You can use get-modification on your service instance that will give you the generated config by your service.

admin@ncs# vpn l3vpn aa get-modifications
cli {
    local-node {
        data  devices {
                   device TEST-IOS-XR-3.01 {
                       config {
                           vrf {
              +                vrf-list aa {
              +                    address-family {
              +                        ipv4 {
              +                            unicast {
              +                                import {
              +                                    route-target {
              +                                        address-list 64512:1;
              +                                    }
              +                                }
              +                                export {
              +                                    route-target {
              +                                        address-list 64512:1;
              +                                    }
              +                                }
              +                            }
              +                        }
              +                    }
              +                }
                           }
                           router {
                               bgp {
              +                    bgp-no-instance 100 {
              +                        vrf aa {
              +                            rd 64512:1;
              +                            address-family {
              +                                ipv4 {
              +                                    unicast {
              +                                    }
              +                                }
              +                            }
              +                            neighbor 2.3.4.5 {
              +                                remote-as 64512;
              +                                address-family {
              +                                    ipv4 {
              +                                        unicast {
              +                                            as-override {
              +                                            }
              +                                        }
              +                                    }
              +                                }
              +                            }
              +                        }
              +                    }
                               }
                           }
                       }
                   }
               }

    }
}
admin@ncs# vpn l3vpn aa get-modifications outformat
Possible completions:
  cli   NCS CLI curly bracket format.
  xml   NETCONF XML edit-config format, i.e., the edit-config that would be
        applied locally (at NCS) to get a config that is equal to that of the
        managed device.
admin@ncs# vpn l3vpn aa get-modifications outformat xml
result-xml {
    local-node {
        data <devices xmlns="http://tail-f.com/ns/ncs">
                <device>
                  <name>TEST-IOS-XR-3.01</name>
                  <config>
                    <vrf xmlns="http://tail-f.com/ned/cisco-ios-xr">
                      <vrf-list>
                        <name>aa</name>
                        <address-family>
                          <ipv4>
                            <unicast>
                              <import>
                                <route-target>
                                  <address-list>
                                    <name>64512:1</name>
                                  </address-list>
                                </route-target>
                              </import>
                              <export>
                                <route-target>
                                  <address-list>
                                    <name>64512:1</name>
                                  </address-list>
                                </route-target>
                              </export>
                            </unicast>
                          </ipv4>
                        </address-family>
                      </vrf-list>
                    </vrf>
                    <router xmlns="http://tail-f.com/ned/cisco-ios-xr">
                      <bgp>
                        <bgp-no-instance>
                          <id>100</id>
                          <vrf>
                            <name>aa</name>
                            <rd>64512:1</rd>
                            <address-family>
                              <ipv4>
                                <unicast/>
                              </ipv4>
                            </address-family>
                            <neighbor>
                              <id>2.3.4.5</id>
                              <remote-as>64512</remote-as>
                              <address-family>
                                <ipv4>
                                  <unicast>
                                    <as-override/>
                                  </unicast>
                                </ipv4>
                              </address-family>
                            </neighbor>
                          </vrf>
                        </bgp-no-instance>
                      </bgp>
                    </router>
                  </config>
                </device>
              </devices>

    }
}