diff --git a/lib/ansible/module_utils/network/cnos/cnos.py b/lib/ansible/module_utils/network/cnos/cnos.py index bd9d571c83..a52e88375a 100644 --- a/lib/ansible/module_utils/network/cnos/cnos.py +++ b/lib/ansible/module_utils/network/cnos/cnos.py @@ -184,6 +184,8 @@ def run_cnos_commands(module, commands, check_rc=True): # Exception in Exceptions if 'VLAN_ACCESS_MAP' in errMsg: return retVal + '<<' + errMsg + '\n' + if 'confederation identifier' in errMsg: + return retVal + '<<' + errMsg + '\n' # Add more here if required retVal = retVal + '<< ' + 'Error-101 ' + errMsg + '\n' return str(retVal) @@ -1361,836 +1363,6 @@ def portChannelConfig(module, prompt, answer): # EOM -def routerConfig( - obj, deviceType, prompt, timeout, protocol, asNum, routerArg1, - routerArg2, routerArg3, routerArg4, routerArg5, routerArg6, routerArg7, - routerArg8): - retVal = "" - # Wait time to get response from server - timeout = timeout - if(protocol == "bgp"): - # bgp config command happens here. - command = "routing-protocol bgp " - value = checkSanityofVariable(deviceType, "bgp_as_number", asNum) - if(value == "ok"): - # BGP command happens here. It creates if not present - command = command + asNum + "\n" - # debugOutput(command) - retVal = waitForDeviceResponse( - command, "(config-router)#", timeout, obj) - retVal = retVal + bgpConfig( - obj, deviceType, "(config-router)#", timeout, routerArg1, - routerArg2, routerArg3, routerArg4, routerArg5, routerArg6, - routerArg7, routerArg8) - else: - retVal = "Error-176" - - elif(protocol == "ospf"): - retVal = "Command Value is Not supported as of now" - - else: - retVal = "Error-177" - - return retVal -# EOM - - -def bgpNeighborAFConfig( - obj, deviceType, prompt, timeout, bgpNeighborAFArg1, bgpNeighborAFArg2, - bgpNeighborAFArg3): - retVal = "" - command = "" - timeout = timeout - if(bgpNeighborAFArg1 == "allowas-in"): - command = command + bgpNeighborAFArg1 + " " - if(bgpNeighborAFArg2 is not None): - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_occurances", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg2 - else: - retVal = "Error-325" - return retVal - else: - command = command - elif(bgpNeighborAFArg1 == "default-originate"): - command = command + bgpNeighborAFArg1 + " " - if(bgpNeighborAFArg2 is not None and bgpNeighborAFArg2 == "route-map"): - command = command + bgpNeighborAFArg2 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_routemap", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg3 - else: - retVal = "Error-324" - return retVal - elif(bgpNeighborAFArg1 == "filter-list"): - command = command + bgpNeighborAFArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_filtername", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg2 + " " - if(bgpNeighborAFArg3 == "in" or bgpNeighborAFArg3 == "out"): - command = command + bgpNeighborAFArg3 - else: - retVal = "Error-323" - return retVal - else: - retVal = "Error-322" - return retVal - - elif(bgpNeighborAFArg1 == "maximum-prefix"): - command = command + bgpNeighborAFArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_maxprefix", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg2 + " " - if(bgpNeighborAFArg3 is not None): - command = command + bgpNeighborAFArg3 - else: - command = command.strip() - else: - retVal = "Error-326" - return retVal - - elif(bgpNeighborAFArg1 == "next-hop-self"): - command = command + bgpNeighborAFArg1 - - elif(bgpNeighborAFArg1 == "prefix-list"): - command = command + bgpNeighborAFArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_prefixname", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg2 + " " - if(bgpNeighborAFArg3 == "in" or bgpNeighborAFArg3 == "out"): - command = command + bgpNeighborAFArg3 - else: - retVal = "Error-321" - return retVal - else: - retVal = "Error-320" - return retVal - - elif(bgpNeighborAFArg1 == "route-map"): - command = command + bgpNeighborAFArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_routemap", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg2 - else: - retVal = "Error-319" - return retVal - elif(bgpNeighborAFArg1 == "route-reflector-client"): - command = command + bgpNeighborAFArg1 - - elif(bgpNeighborAFArg1 == "send-community"): - command = command + bgpNeighborAFArg1 + " " - if(bgpNeighborAFArg2 is not None and bgpNeighborAFArg2 == "extended"): - command = command + bgpNeighborAFArg2 - else: - command = command - - elif(bgpNeighborAFArg1 == "soft-reconfiguration"): - command = command + bgpNeighborAFArg1 + " inbound" - - elif(bgpNeighborAFArg1 == "unsuppress-map"): - command = command + bgpNeighborAFArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_af_routemap", bgpNeighborAFArg2) - if(value == "ok"): - command = command + bgpNeighborAFArg2 - else: - retVal = "Error-318" - return retVal - - else: - retVal = "Error-317" - return retVal - - command = command + "\n" - # debugOutput(command) - retVal = retVal + waitForDeviceResponse(command, prompt, timeout, obj) - command = "exit \n" - retVal = retVal + \ - waitForDeviceResponse( - command, "(config-router-neighbor)#", timeout, obj) - return retVal -# EOM - - -def bgpNeighborConfig( - obj, deviceType, prompt, timeout, bgpNeighborArg1, bgpNeighborArg2, - bgpNeighborArg3, bgpNeighborArg4, bgpNeighborArg5): - retVal = "" - command = "" - timeout = timeout - - if(bgpNeighborArg1 == "address-family"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_address_family", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 + " unicast \n" - # debugOutput(command) - retVal = waitForDeviceResponse( - command, "(config-router-neighbor-af)#", timeout, obj) - retVal = retVal + bgpNeighborAFConfig( - obj, deviceType, "(config-router-neighbor-af)#", timeout, - bgpNeighborArg3, bgpNeighborArg4, bgpNeighborArg5) - return retVal - else: - retVal = "Error-316" - return retVal - - elif(bgpNeighborArg1 == "advertisement-interval"): - command = command + bgpNeighborArg1 - - elif(bgpNeighborArg1 == "bfd"): - command = command + bgpNeighborArg1 + " " - if(bgpNeighborArg2 is not None and bgpNeighborArg2 == "mutihop"): - command = command + bgpNeighborArg2 - else: - command = command - - elif(bgpNeighborArg1 == "connection-retry-time"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_connection_retrytime", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-315" - return retVal - - elif(bgpNeighborArg1 == "description"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_description", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-314" - return retVal - - elif(bgpNeighborArg1 == "disallow-infinite-holdtime"): - command = command + bgpNeighborArg1 - - elif(bgpNeighborArg1 == "dont-capability-negotiate"): - command = command + bgpNeighborArg1 - - elif(bgpNeighborArg1 == "dynamic-capability"): - command = command + bgpNeighborArg1 - - elif(bgpNeighborArg1 == "ebgp-multihop"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_maxhopcount", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-313" - return retVal - elif(bgpNeighborArg1 == "interface"): - command = command + bgpNeighborArg1 + " " - # TBD - elif(bgpNeighborArg1 == "local-as"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_local_as", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 + " " - if(bgpNeighborArg3 is not None and - bgpNeighborArg3 == "no-prepend"): - command = command + bgpNeighborArg3 + " " - if(bgpNeighborArg4 is not None and - bgpNeighborArg4 == "replace-as"): - command = command + bgpNeighborArg4 + " " - if(bgpNeighborArg5 is not None and - bgpNeighborArg5 == "dual-as"): - command = command + bgpNeighborArg5 - else: - command = command.strip() - else: - command = command.strip() - else: - command = command.strip() - else: - retVal = "Error-312" - return retVal - - elif(bgpNeighborArg1 == "maximum-peers"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_maxpeers", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-311" - return retVal - - elif(bgpNeighborArg1 == "password"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_password", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-310" - return retVal - - elif(bgpNeighborArg1 == "remove-private-AS"): - command = command + bgpNeighborArg1 - - elif(bgpNeighborArg1 == "timers"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_timers_Keepalive", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_timers_holdtime", bgpNeighborArg3) - if(value == "ok"): - command = command + bgpNeighborArg3 - else: - retVal = "Error-309" - return retVal - else: - retVal = "Error-308" - return retVal - - elif(bgpNeighborArg1 == "transport"): - command = command + bgpNeighborArg1 + " connection-mode passive " - - elif(bgpNeighborArg1 == "ttl-security"): - command = command + bgpNeighborArg1 + " hops " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_ttl_hops", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-307" - return retVal - - elif(bgpNeighborArg1 == "update-source"): - command = command + bgpNeighborArg1 + " " - if(bgpNeighborArg2 is not None): - value = checkSanityofVariable( - deviceType, "bgp_neighbor_update_options", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 + " " - if(bgpNeighborArg2 == "ethernet"): - value = checkSanityofVariable( - deviceType, "bgp_neighbor_update_ethernet", - bgpNeighborArg3) - if(value == "ok"): - command = command + bgpNeighborArg3 - else: - retVal = "Error-304" - return retVal - elif(bgpNeighborArg2 == "loopback"): - value = checkSanityofVariable( - deviceType, "bgp_neighbor_update_loopback", - bgpNeighborArg3) - if(value == "ok"): - command = command + bgpNeighborArg3 - else: - retVal = "Error-305" - return retVal - else: - value = checkSanityofVariable( - deviceType, "bgp_neighbor_update_vlan", - bgpNeighborArg3) - if(value == "ok"): - command = command + bgpNeighborArg3 - else: - retVal = "Error-306" - return retVal - else: - command = command + bgpNeighborArg2 - else: - retVal = "Error-303" - return retVal - - elif(bgpNeighborArg1 == "weight"): - command = command + bgpNeighborArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_neighbor_weight", bgpNeighborArg2) - if(value == "ok"): - command = command + bgpNeighborArg2 - else: - retVal = "Error-302" - return retVal - - else: - retVal = "Error-301" - return retVal - - command = command + "\n" - # debugOutput(command) - retVal = retVal + waitForDeviceResponse(command, prompt, timeout, obj) - command = "exit \n" - retVal = retVal + \ - waitForDeviceResponse(command, "(config-router)#", timeout, obj) - return retVal -# EOM - - -def bgpAFConfig( - obj, deviceType, prompt, timeout, bgpAFArg1, bgpAFArg2, bgpAFArg3, - bgpAFArg4, bgpAFArg5, bgpAFArg6): - retVal = "" - command = "" - timeout = timeout - if(bgpAFArg1 == "aggregate-address"): - command = command + bgpAFArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_aggregate_prefix", bgpAFArg2) - if(value == "ok"): - if(bgpAFArg2 is None): - command = command.strip() - elif(bgpAFArg2 == "as-set" or bgpAFArg2 == "summary-only"): - command = command + bgpAFArg2 + " " - if((bgpAFArg3 is not None) and (bgpAFArg2 == "as-set")): - command = command + "summary-only" - else: - retVal = "Error-297" - return retVal - else: - retVal = "Error-296" - return retVal - elif(bgpAFArg1 == "client-to-client"): - command = command + bgpAFArg1 + " reflection " - elif(bgpAFArg1 == "dampening"): - command = command + bgpAFArg1 + " " - if(bgpAFArg2 == "route-map"): - command = command + bgpAFArg2 + " " - value = checkSanityofVariable( - deviceType, "addrfamily_routemap_name", bgpAFArg3) - if(value == "ok"): - command = command + bgpAFArg3 - else: - retVal = "Error-196" - return retVal - elif(bgpAFArg2 is not None): - value = checkSanityofVariable( - deviceType, "reachability_half_life", bgpAFArg2) - if(value == "ok"): - command = command + bgpAFArg2 + " " - if(bgpAFArg3 is not None): - value1 = checkSanityofVariable( - deviceType, "start_reuse_route_value", bgpAFArg3) - value2 = checkSanityofVariable( - deviceType, "start_suppress_route_value", bgpAFArg4) - value3 = checkSanityofVariable( - deviceType, "max_duration_to_suppress_route", - bgpAFArg5) - if(value1 == "ok" and value2 == "ok" and value3 == "ok"): - command = command + bgpAFArg3 + " " + bgpAFArg4 + \ - " " + bgpAFArg5 + " " - if(bgpAFArg6 is not None): - value = checkSanityofVariable( - deviceType, - "unreachability_halftime_for_penalty", - bgpAFArg6) - if(value == "ok"): - command = command + bgpAFArg6 - else: - retVal = "Error-295" - return retVal - else: - command = command.strip() - else: - retVal = "Error-294" - return retVal - - elif(bgpAFArg1 == "distance"): - command = command + bgpAFArg1 + " " - value = checkSanityofVariable( - deviceType, "distance_external_AS", bgpAFArg2) - if(value == "ok"): - command = command + bgpAFArg2 + " " - value = checkSanityofVariable( - deviceType, "distance_internal_AS", bgpAFArg3) - if(value == "ok"): - command = command + bgpAFArg3 + " " - value = checkSanityofVariable( - deviceType, "distance_local_routes", bgpAFArg4) - if(value == "ok"): - command = command + bgpAFArg4 - else: - retVal = "Error-291" - return retVal - else: - retVal = "Error-292" - return retVal - else: - retVal = "Error-293" - return retVal - elif(bgpAFArg1 == "maximum-paths"): - command = command + bgpAFArg1 + " " - value = checkSanityofVariable(deviceType, "maxpath_option", bgpAFArg2) - if(value == "ok"): - command = command + bgpAFArg2 + " " - value = checkSanityofVariable( - deviceType, "maxpath_numbers", bgpAFArg3) - if(value == "ok"): - command = command + bgpAFArg3 - else: - retVal = "Error-199" - return retVal - else: - retVal = "Error-290" - return retVal - - elif(bgpAFArg1 == "network"): - command = command + bgpAFArg1 + " " - if(bgpAFArg2 == "synchronization"): - command = command + bgpAFArg2 - else: - value = checkSanityofVariable( - deviceType, "network_ip_prefix_with_mask", bgpAFArg2) - if(value == "ok"): - command = command + bgpAFArg2 + " " - if(bgpAFArg3 is not None and bgpAFArg3 == "backdoor"): - command = command + bgpAFArg3 - elif(bgpAFArg3 is not None and bgpAFArg3 == "route-map"): - command = command + bgpAFArg3 - value = checkSanityofVariable( - deviceType, "addrfamily_routemap_name", bgpAFArg4) - if(value == "ok"): - command = command + bgpAFArg4 + " " - if(bgpAFArg5 is not None and bgpAFArg5 == "backdoor"): - command = command + bgpAFArg5 - else: - retVal = "Error-298" - return retVal - else: - retVal = "Error-196" - return retVal - else: - command = command.strip() - else: - value = checkSanityofVariable( - deviceType, "network_ip_prefix_value", bgpAFArg2) - if(value == "ok"): - command = command + bgpAFArg2 + " " - if(bgpAFArg3 is not None and bgpAFArg3 == "backdoor"): - command = command + bgpAFArg3 - elif(bgpAFArg3 is not None and bgpAFArg3 == "route-map"): - command = command + bgpAFArg3 - value = checkSanityofVariable( - deviceType, "addrfamily_routemap_name", bgpAFArg4) - if(value == "ok"): - command = command + bgpAFArg4 + " " - if(bgpAFArg5 is not None and - bgpAFArg5 == "backdoor"): - command = command + bgpAFArg5 - else: - retVal = "Error-298" - return retVal - else: - retVal = "Error-196" - return retVal - elif(bgpAFArg3 is not None and bgpAFArg3 == "mask"): - command = command + bgpAFArg3 - value = checkSanityofVariable( - deviceType, "network_ip_prefix_mask", bgpAFArg4) - if(value == "ok"): - command = command + bgpAFArg4 + " " - else: - retVal = "Error-299" - return retVal - else: - command = command.strip() - else: - retVal = "Error-300" - return retVal - - elif(bgpAFArg1 == "nexthop"): - command = command + bgpAFArg1 + " trigger-delay critical " - value = checkSanityofVariable( - deviceType, "nexthop_crtitical_delay", bgpAFArg2) - if(value == "ok"): - command = command + bgpAFArg2 + " " - value = checkSanityofVariable( - deviceType, "nexthop_noncrtitical_delay", bgpAFArg3) - if(value == "ok"): - command = command + bgpAFArg3 + " " - else: - retVal = "Error-198" - return retVal - else: - retVal = "Error-197" - return retVal - elif(bgpAFArg1 == "redistribute"): - command = command + bgpAFArg1 + " " - value = checkSanityofVariable( - deviceType, "addrfamily_redistribute_option", bgpAFArg2) - if(value == "ok"): - if(bgpAFArg2 is not None): - command = command + bgpAFArg2 + " " + "route-map " - value = checkSanityofVariable( - deviceType, "addrfamily_routemap_name", bgpAFArg3) - if(value == "ok"): - command = command + bgpAFArg3 - else: - retVal = "Error-196" - return retVal - else: - retVal = "Error-195" - return retVal - elif(bgpAFArg1 == "save" or bgpAFArg1 == "synchronization"): - command = command + bgpAFArg1 - else: - retVal = "Error-194" - return retVal - command = command + "\n" - # debugOutput(command) - retVal = retVal + waitForDeviceResponse(command, prompt, timeout, obj) - command = "exit \n" - retVal = retVal + \ - waitForDeviceResponse(command, "(config-router)#", timeout, obj) - return retVal -# EOM - - -def bgpConfig( - obj, deviceType, prompt, timeout, bgpArg1, bgpArg2, bgpArg3, bgpArg4, - bgpAgr5, bgpArg6, bgpArg7, bgpArg8): - retVal = "" - command = "" - # Wait time to get response from server - timeout = timeout - if(bgpArg1 == "address-family"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - value = checkSanityofVariable( - deviceType, "bgp_address_family", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 + " " + "unicast \n" - debugOutput(command) - retVal = waitForDeviceResponse( - command, "(config-router-af)#", timeout, obj) - retVal = retVal + bgpAFConfig( - obj, deviceType, "(config-router-af)#", timeout, - bgpArg3, bgpArg4, bgpAgr5, bgpArg6, bgpArg7, bgpArg8) - return retVal - else: - retVal = "Error-178" - return retVal - - elif(bgpArg1 == "bestpath"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - if(bgpArg2 == "always-compare-med"): - # debugOutput(bgpArg2) - command = command + bgpArg2 - elif(bgpArg2 == "compare-confed-aspath"): - # debugOutput(bgpArg2) - command = command + bgpArg2 - elif(bgpArg2 == "compare-routerid"): - # debugOutput(bgpArg2) - command = command + bgpArg2 - elif(bgpArg2 == "dont-compare-originator-id"): - # debugOutput(bgpArg2) - command = command + bgpArg2 - elif(bgpArg2 == "tie-break-on-age"): - # debugOutput(bgpArg2) - command = command + bgpArg2 - elif(bgpArg2 == "as-path"): - # debugOutput(bgpArg2) - command = command + bgpArg2 + " " - if(bgpArg3 == "ignore" or bgpArg3 == "multipath-relax"): - command = command + bgpArg3 - else: - retVal = "Error-179" - return retVal - elif(bgpArg2 == "med"): - # debugOutput(bgpArg2) - command = command + bgpArg2 + " " - if(bgpArg3 == "confed" or - bgpArg3 == "missing-as-worst" or - bgpArg3 == "non-deterministic" or - bgpArg3 == "remove-recv-med" or - bgpArg3 == "remove-send-med"): - command = command + bgpArg3 - else: - retVal = "Error-180" - return retVal - else: - retVal = "Error-181" - return retVal - - elif(bgpArg1 == "bgp"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " as-local-count " - value = checkSanityofVariable( - deviceType, "bgp_bgp_local_count", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - retVal = "Error-182" - return retVal - - elif(bgpArg1 == "cluster-id"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - value = checkSanityofVariable(deviceType, "cluster_id_as_ip", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - value = checkSanityofVariable( - deviceType, "cluster_id_as_number", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - retVal = "Error-183" - return retVal - - elif(bgpArg1 == "confederation"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - if(bgpArg2 == "identifier"): - value = checkSanityofVariable( - deviceType, "confederation_identifier", bgpArg3) - if(value == "ok"): - command = command + " " + bgpArg2 + " " + bgpArg3 - else: - retVal = "Error-184" - return retVal - elif(bgpArg2 == "peers"): - value = checkSanityofVariable( - deviceType, "confederation_peers_as", bgpArg3) - if(value == "ok"): - command = command + " " + bgpArg2 + " " + bgpArg3 - else: - retVal = "Error-185" - return retVal - else: - retVal = "Error-186" - return retVal - - elif(bgpArg1 == "enforce-first-as"): - # debugOutput(bgpArg1) - command = command + bgpArg1 - - elif(bgpArg1 == "fast-external-failover"): - # debugOutput(bgpArg1) - command = command + bgpArg1 - - elif(bgpArg1 == "graceful-restart"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " stalepath-time " - value = checkSanityofVariable( - deviceType, "stalepath_delay_value", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - retVal = "Error-187" - return retVal - - elif(bgpArg1 == "graceful-restart-helper"): - # debugOutput(bgpArg1) - command = command + bgpArg1 - - elif(bgpArg1 == "log-neighbor-changes"): - # debugOutput(bgpArg1) - command = command + bgpArg1 - - elif(bgpArg1 == "maxas-limit"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - value = checkSanityofVariable(deviceType, "maxas_limit_as", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - retVal = "Error-188" - return retVal - - elif(bgpArg1 == "neighbor"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - value = checkSanityofVariable( - deviceType, "neighbor_ipaddress", bgpArg2) - # retVal = "Error-102" - # return retVal - if(value == "ok"): - command = command + bgpArg2 - if(bgpArg3 is not None): - command = command + " remote-as " - value = checkSanityofVariable( - deviceType, "neighbor_as", bgpArg3) - if(value == "ok"): - command = command + bgpArg3 + "\n" - # debugOutput(command) - retVal = waitForDeviceResponse( - command, "(config-router-neighbor)#", timeout, obj) - retVal = retVal + bgpNeighborConfig( - obj, deviceType, "(config-router-neighbor)#", - timeout, bgpArg4, bgpAgr5, bgpArg6, bgpArg7, bgpArg8) - return retVal - else: - retVal = "Error-189" - return retVal - - elif(bgpArg1 == "router-id"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " " - value = checkSanityofVariable(deviceType, "router_id", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - retVal = "Error-190" - return retVal - - elif(bgpArg1 == "shutdown"): - # debugOutput(bgpArg1) - command = command + bgpArg1 - - elif(bgpArg1 == "synchronization"): - # debugOutput(bgpArg1) - command = command + bgpArg1 - - elif(bgpArg1 == "timers"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " bgp " - value = checkSanityofVariable( - deviceType, "bgp_keepalive_interval", bgpArg2) - if(value == "ok"): - command = command + bgpArg2 - else: - retVal = "Error-191" - return retVal - if(bgpArg3 is not None): - value = checkSanityofVariable(deviceType, "bgp_holdtime", bgpArg3) - if(value == "ok"): - command = command + " " + bgpArg3 - else: - retVal = "Error-192" - return retVal - else: - retVal = "Error-192" - return retVal - - elif(bgpArg1 == "vrf"): - # debugOutput(bgpArg1) - command = command + bgpArg1 + " default" - else: - # debugOutput(bgpArg1) - retVal = "Error-192" - return retVal - command = command + "\n" - # debugOutput(command) - retVal = retVal + waitForDeviceResponse(command, prompt, timeout, obj) - # Come back to config mode - command = "exit \n" - # debugOutput(command) - retVal = retVal + waitForDeviceResponse(command, "(config)#", timeout, obj) - - return retVal -# EOM - - def vlanConfig(module, prompt, answer): retVal = '' @@ -3657,7 +2829,7 @@ def checkLong(s): def debugOutput(command): - f = open('debugOuput.txt', 'a') + f = open('debugOutput.txt', 'a') f.write(str(command)) # python will convert \n to os.linesep f.close() # you can omit in most cases as the destructor will call it # EOM diff --git a/lib/ansible/modules/network/cnos/cnos_bgp.py b/lib/ansible/modules/network/cnos/cnos_bgp.py index 44cb378bd0..726acf52d4 100644 --- a/lib/ansible/modules/network/cnos/cnos_bgp.py +++ b/lib/ansible/modules/network/cnos/cnos_bgp.py @@ -32,21 +32,24 @@ DOCUMENTATION = ''' --- module: cnos_bgp author: "Anil Kumar Muraleedharan (@amuraleedhar)" -short_description: Manage BGP resources and attributes on devices running Lenovo CNOS +short_description: Manage BGP resources and attributes on devices running CNOS description: - - This module allows you to work with Border Gateway Protocol (BGP) related configurations. - The operators used are overloaded to ensure control over switch BGP configurations. This - module is invoked using method with asNumber as one of its arguments. The first level of - the BGP configuration allows to set up an AS number, with the following attributes going - into various configuration operations under the context of BGP. After passing this level, - there are eight BGP arguments that will perform further configurations. They are bgpArg1, - bgpArg2, bgpArg3, bgpArg4, bgpArg5, bgpArg6, bgpArg7, and bgpArg8. For more details on - how to use these arguments, see [Overloaded Variables]. + - This module allows you to work with Border Gateway Protocol (BGP) related + configurations. The operators used are overloaded to ensure control over + switch BGP configurations. This module is invoked using method with + asNumber as one of its arguments. The first level of the BGP configuration + allows to set up an AS number, with the following attributes going + into various configuration operations under the context of BGP. + After passing this level, there are eight BGP arguments that will perform + further configurations. They are bgpArg1, bgpArg2, bgpArg3, bgpArg4, + bgpArg5, bgpArg6, bgpArg7, and bgpArg8. For more details on how to use + these arguments, see [Overloaded Variables]. This module uses SSH to manage network device configuration. The results of the operation will be placed in a directory named 'results' - that must be created by the user in their local directory to where the playbook is run. - For more information about this module from Lenovo and customizing it usage for your - use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_bgp.html) + that must be created by the user in their local directory to where the + playbook is run. For more information about this module from Lenovo and + customizing it usage for your use cases, please visit + U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_bgp.html) version_added: "2.3" extends_documentation_fragment: cnos options: @@ -57,62 +60,83 @@ options: default: Null bgpArg1: description: - - This is an overloaded bgp first argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp first argument. Usage of this argument + can be found is the User Guide referenced above. required: Yes default: Null - choices: [address-family,bestpath,bgp,cluster-id,confederation,enforce-first-as,fast-external-failover, - graceful-restart,graceful-restart-helper,log-neighbor-changes,maxas-limit,neighbor,router-id,shutdown, - synchronization,timers,vrf] + choices: [address-family,bestpath,bgp,cluster-id,confederation, + enforce-first-as,fast-external-failover,graceful-restart, + graceful-restart-helper,log-neighbor-changes, + maxas-limit,neighbor,router-id,shutdown,synchronization, + timers,vrf] bgpArg2: description: - - This is an overloaded bgp second argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp second argument. Usage of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [ipv4 or ipv6, always-compare-med,compare-confed-aspath,compare-routerid,dont-compare-originator-id,tie-break-on-age, - as-path,med,identifier,peers] + choices: [ipv4 or ipv6, always-compare-med,compare-confed-aspath, + compare-routerid,dont-compare-originator-id,tie-break-on-age, + as-path,med,identifier,peers] bgpArg3: description: - - This is an overloaded bgp third argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp third argument. Usage of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [aggregate-address,client-to-client,dampening,distance,maximum-paths,network,nexthop,redistribute,save,synchronization, - ignore or multipath-relax, confed or missing-as-worst or non-deterministic or remove-recv-med or remove-send-med] + choices: [aggregate-address,client-to-client,dampening,distance, + maximum-paths,network,nexthop,redistribute,save, + synchronization,ignore or multipath-relax, + confed or missing-as-worst or non-deterministic or + remove-recv-med or remove-send-med] bgpArg4: description: - - This is an overloaded bgp fourth argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp fourth argument. Usage of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [Aggregate prefix, Reachability Half-life time,route-map, Distance for routes external,ebgp or ibgp, - IP prefix ,IP prefix /, synchronization, Delay value, direct, ospf, static, memory] + choices: [Aggregate prefix, Reachability Half-life time,route-map, + Distance for routes ext,ebgp or ibgp,IP prefix , + IP prefix /, synchronization, + Delay value, direct, ospf, static, memory] bgpArg5: description: - - This is an overloaded bgp fifth argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp fifth argument. Usage of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [as-set, summary-only, Value to start reusing a route, Distance for routes internal, Supported multipath numbers, - backdoor, map, route-map ] + choices: [as-set, summary-only, Value to start reusing a route, + Distance for routes internal, Supported multipath numbers, + backdoor, map, route-map ] bgpArg6: description: - - This is an overloaded bgp sixth argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp sixth argument. Usage of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [summary-only,as-set, route-map name, Value to start suppressing a route, Distance for local routes, Network mask, - Pointer to route-map entries] + choices: [summary-only,as-set, route-map name, + Value to start suppressing a route, Distance local routes, + Network mask, Pointer to route-map entries] bgpArg7: description: - - This is an overloaded bgp seventh argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp seventh argument. Use of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [ Maximum duration to suppress a stable route(minutes), backdoor,route-map, Name of the route map ] + choices: [Maximum duration to suppress a stable route(minutes), + backdoor,route-map, Name of the route map ] bgpArg8: description: - - This is an overloaded bgp eigth argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded bgp eigth argument. Usage of this argument + can be found is the User Guide referenced above. required: No default: Null - choices: [ Un-reachability Half-life time for the penalty(minutes), backdoor] + choices: [Un-reachability Half-life time for the penalty(minutes), + backdoor] ''' EXAMPLES = ''' -Tasks: The following are examples of using the module cnos_bgp. These are written in the main.yml file of the tasks directory. +Tasks: The following are examples of using the module cnos_bgp. These are + written in the main.yml file of the tasks directory. --- - name: Test BGP - neighbor cnos_bgp: @@ -361,17 +385,13 @@ Tasks: The following are examples of using the module cnos_bgp. These are writte ''' RETURN = ''' msg: - description: Success or failure message. Upon any failure, the method returns an error display string. + description: Success or failure message. Upon any failure, the method returns + an error display string. returned: always type: string ''' import sys -try: - import paramiko - HAS_PARAMIKO = True -except ImportError: - HAS_PARAMIKO = False import time import socket import array @@ -387,6 +407,812 @@ from ansible.module_utils.basic import AnsibleModule from collections import defaultdict +def bgpNeighborConfig(module, cmd, prompt, answer): + retVal = '' + command = '' + bgpNeighborArg1 = module.params['bgpArg4'] + bgpNeighborArg2 = module.params['bgpArg5'] + bgpNeighborArg3 = module.params['bgpArg6'] + bgpNeighborArg4 = module.params['bgpArg7'] + bgpNeighborArg5 = module.params['bgpArg8'] + deviceType = module.params['deviceType'] + + if(bgpNeighborArg1 == "address-family"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_address_family", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + " unicast" + # debugOutput(command) + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + bgpNeighborAFConfig(module, cmd, '(config-router-neighbor-af)#', answer) + return retVal + else: + retVal = "Error-316" + return retVal + + elif(bgpNeighborArg1 == "advertisement-interval"): + command = command + bgpNeighborArg1 + + elif(bgpNeighborArg1 == "bfd"): + command = command + bgpNeighborArg1 + " " + if(bgpNeighborArg2 is not None and bgpNeighborArg2 == "mutihop"): + command = command + bgpNeighborArg2 + else: + command = command + + elif(bgpNeighborArg1 == "connection-retry-time"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_connection_retrytime", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-315" + return retVal + + elif(bgpNeighborArg1 == "description"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_description", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-314" + return retVal + + elif(bgpNeighborArg1 == "disallow-infinite-holdtime"): + command = command + bgpNeighborArg1 + + elif(bgpNeighborArg1 == "dont-capability-negotiate"): + command = command + bgpNeighborArg1 + + elif(bgpNeighborArg1 == "dynamic-capability"): + command = command + bgpNeighborArg1 + + elif(bgpNeighborArg1 == "ebgp-multihop"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_maxhopcount", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-313" + return retVal + + elif(bgpNeighborArg1 == "interface"): + command = command + bgpNeighborArg1 + " " + # TBD + + elif(bgpNeighborArg1 == "local-as"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_local_as", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + " " + if(bgpNeighborArg3 is not None and + bgpNeighborArg3 == "no-prepend"): + command = command + bgpNeighborArg3 + " " + if(bgpNeighborArg4 is not None and + bgpNeighborArg4 == "replace-as"): + command = command + bgpNeighborArg4 + " " + if(bgpNeighborArg5 is not None and + bgpNeighborArg5 == "dual-as"): + command = command + bgpNeighborArg5 + else: + command = command.strip() + else: + command = command.strip() + else: + command = command.strip() + else: + retVal = "Error-312" + return retVal + + elif(bgpNeighborArg1 == "maximum-peers"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_maxpeers", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-311" + return retVal + + elif(bgpNeighborArg1 == "password"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_password", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-310" + return retVal + + elif(bgpNeighborArg1 == "remove-private-AS"): + command = command + bgpNeighborArg1 + + elif(bgpNeighborArg1 == "timers"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_timers_Keepalive", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_timers_holdtime", bgpNeighborArg3) + if(value == "ok"): + command = command + bgpNeighborArg3 + else: + retVal = "Error-309" + return retVal + else: + retVal = "Error-308" + return retVal + + elif(bgpNeighborArg1 == "transport"): + command = command + bgpNeighborArg1 + " connection-mode passive " + + elif(bgpNeighborArg1 == "ttl-security"): + command = command + bgpNeighborArg1 + " hops " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_ttl_hops", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-307" + return retVal + + elif(bgpNeighborArg1 == "update-source"): + command = command + bgpNeighborArg1 + " " + if(bgpNeighborArg2 is not None): + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_update_options", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + " " + if(bgpNeighborArg2 == "ethernet"): + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_update_ethernet", + bgpNeighborArg3) + if(value == "ok"): + command = command + bgpNeighborArg3 + else: + retVal = "Error-304" + return retVal + elif(bgpNeighborArg2 == "loopback"): + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_update_loopback", + bgpNeighborArg3) + if(value == "ok"): + command = command + bgpNeighborArg3 + else: + retVal = "Error-305" + return retVal + else: + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_update_vlan", + bgpNeighborArg3) + if(value == "ok"): + command = command + bgpNeighborArg3 + else: + retVal = "Error-306" + return retVal + else: + command = command + bgpNeighborArg2 + else: + retVal = "Error-303" + return retVal + + elif(bgpNeighborArg1 == "weight"): + command = command + bgpNeighborArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_weight", bgpNeighborArg2) + if(value == "ok"): + command = command + bgpNeighborArg2 + else: + retVal = "Error-302" + return retVal + + else: + retVal = "Error-301" + return retVal + + # debugOutput(command) + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + command = "exit \n" + return retVal +# EOM + + +def bgpNeighborAFConfig(module, cmd, prompt, answer): + retVal = '' + command = '' + bgpNeighborAFArg1 = module.params['bgpArg6'] + bgpNeighborAFArg2 = module.params['bgpArg7'] + bgpNeighborAFArg3 = module.params['bgpArg8'] + deviceType = module.params['deviceType'] + if(bgpNeighborAFArg1 == "allowas-in"): + command = command + bgpNeighborAFArg1 + " " + if(bgpNeighborAFArg2 is not None): + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_occurances", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg2 + else: + retVal = "Error-325" + return retVal + else: + command = command + + elif(bgpNeighborAFArg1 == "default-originate"): + command = command + bgpNeighborAFArg1 + " " + if(bgpNeighborAFArg2 is not None and bgpNeighborAFArg2 == "route-map"): + command = command + bgpNeighborAFArg2 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_routemap", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg3 + else: + retVal = "Error-324" + return retVal + + elif(bgpNeighborAFArg1 == "filter-list"): + command = command + bgpNeighborAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_filtername", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg2 + " " + if(bgpNeighborAFArg3 == "in" or bgpNeighborAFArg3 == "out"): + command = command + bgpNeighborAFArg3 + else: + retVal = "Error-323" + return retVal + else: + retVal = "Error-322" + return retVal + + elif(bgpNeighborAFArg1 == "maximum-prefix"): + command = command + bgpNeighborAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_maxprefix", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg2 + " " + if(bgpNeighborAFArg3 is not None): + command = command + bgpNeighborAFArg3 + else: + command = command.strip() + else: + retVal = "Error-326" + return retVal + + elif(bgpNeighborAFArg1 == "next-hop-self"): + command = command + bgpNeighborAFArg1 + + elif(bgpNeighborAFArg1 == "prefix-list"): + command = command + bgpNeighborAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_prefixname", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg2 + " " + if(bgpNeighborAFArg3 == "in" or bgpNeighborAFArg3 == "out"): + command = command + bgpNeighborAFArg3 + else: + retVal = "Error-321" + return retVal + else: + retVal = "Error-320" + return retVal + + elif(bgpNeighborAFArg1 == "route-map"): + command = command + bgpNeighborAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_routemap", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg2 + else: + retVal = "Error-319" + return retVal + elif(bgpNeighborAFArg1 == "route-reflector-client"): + command = command + bgpNeighborAFArg1 + + elif(bgpNeighborAFArg1 == "send-community"): + command = command + bgpNeighborAFArg1 + " " + if(bgpNeighborAFArg2 is not None and bgpNeighborAFArg2 == "extended"): + command = command + bgpNeighborAFArg2 + else: + command = command + + elif(bgpNeighborAFArg1 == "soft-reconfiguration"): + command = command + bgpNeighborAFArg1 + " inbound" + + elif(bgpNeighborAFArg1 == "unsuppress-map"): + command = command + bgpNeighborAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_neighbor_af_routemap", bgpNeighborAFArg2) + if(value == "ok"): + command = command + bgpNeighborAFArg2 + else: + retVal = "Error-318" + return retVal + + else: + retVal = "Error-317" + return retVal + + # debugOutput(command) + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + return retVal +# EOM + + +def bgpAFConfig(module, cmd, prompt, answer): + retVal = '' + command = '' + bgpAFArg1 = module.params['bgpArg3'] + bgpAFArg2 = module.params['bgpArg4'] + bgpAFArg3 = module.params['bgpArg5'] + bgpAFArg4 = module.params['bgpArg6'] + bgpAFArg5 = module.params['bgpArg7'] + bgpAFArg6 = module.params['bgpArg8'] + deviceType = module.params['deviceType'] + if(bgpAFArg1 == "aggregate-address"): + command = command + bgpAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_aggregate_prefix", bgpAFArg2) + if(value == "ok"): + if(bgpAFArg2 is None): + command = command.strip() + elif(bgpAFArg2 == "as-set" or bgpAFArg2 == "summary-only"): + command = command + bgpAFArg2 + " " + if((bgpAFArg3 is not None) and (bgpAFArg2 == "as-set")): + command = command + "summary-only" + else: + retVal = "Error-297" + return retVal + else: + retVal = "Error-296" + return retVal + + elif(bgpAFArg1 == "client-to-client"): + command = command + bgpAFArg1 + " reflection " + + elif(bgpAFArg1 == "dampening"): + command = command + bgpAFArg1 + " " + if(bgpAFArg2 == "route-map"): + command = command + bgpAFArg2 + " " + value = cnos.checkSanityofVariable( + deviceType, "addrfamily_routemap_name", bgpAFArg3) + if(value == "ok"): + command = command + bgpAFArg3 + else: + retVal = "Error-196" + return retVal + elif(bgpAFArg2 is not None): + value = cnos.checkSanityofVariable( + deviceType, "reachability_half_life", bgpAFArg2) + if(value == "ok"): + command = command + bgpAFArg2 + " " + if(bgpAFArg3 is not None): + value1 = cnos.checkSanityofVariable( + deviceType, "start_reuse_route_value", bgpAFArg3) + value2 = cnos.checkSanityofVariable( + deviceType, "start_suppress_route_value", bgpAFArg4) + value3 = cnos.checkSanityofVariable( + deviceType, "max_duration_to_suppress_route", + bgpAFArg5) + if(value1 == "ok" and value2 == "ok" and value3 == "ok"): + command = command + bgpAFArg3 + " " + bgpAFArg4 + \ + " " + bgpAFArg5 + " " + if(bgpAFArg6 is not None): + value = cnos.checkSanityofVariable( + deviceType, + "unreachability_halftime_for_penalty", + bgpAFArg6) + if(value == "ok"): + command = command + bgpAFArg6 + else: + retVal = "Error-295" + return retVal + else: + command = command.strip() + else: + retVal = "Error-294" + return retVal + + elif(bgpAFArg1 == "distance"): + command = command + bgpAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "distance_external_AS", bgpAFArg2) + if(value == "ok"): + command = command + bgpAFArg2 + " " + value = cnos.checkSanityofVariable( + deviceType, "distance_internal_AS", bgpAFArg3) + if(value == "ok"): + command = command + bgpAFArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "distance_local_routes", bgpAFArg4) + if(value == "ok"): + command = command + bgpAFArg4 + else: + retVal = "Error-291" + return retVal + else: + retVal = "Error-292" + return retVal + else: + retVal = "Error-293" + return retVal + + elif(bgpAFArg1 == "maximum-paths"): + command = command + bgpAFArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "maxpath_option", bgpAFArg2) + if(value == "ok"): + command = command + bgpAFArg2 + " " + value = cnos.checkSanityofVariable( + deviceType, "maxpath_numbers", bgpAFArg3) + if(value == "ok"): + command = command + bgpAFArg3 + else: + retVal = "Error-199" + return retVal + else: + retVal = "Error-290" + return retVal + + elif(bgpAFArg1 == "network"): + command = command + bgpAFArg1 + " " + if(bgpAFArg2 == "synchronization"): + command = command + bgpAFArg2 + else: + value = cnos.checkSanityofVariable( + deviceType, "network_ip_prefix_with_mask", bgpAFArg2) + if(value == "ok"): + command = command + bgpAFArg2 + " " + if(bgpAFArg3 is not None and bgpAFArg3 == "backdoor"): + command = command + bgpAFArg3 + elif(bgpAFArg3 is not None and bgpAFArg3 == "route-map"): + command = command + bgpAFArg3 + value = cnos.checkSanityofVariable( + deviceType, "addrfamily_routemap_name", bgpAFArg4) + if(value == "ok"): + command = command + bgpAFArg4 + " " + if(bgpAFArg5 is not None and bgpAFArg5 == "backdoor"): + command = command + bgpAFArg5 + else: + retVal = "Error-298" + return retVal + else: + retVal = "Error-196" + return retVal + else: + command = command.strip() + else: + value = cnos.checkSanityofVariable( + deviceType, "network_ip_prefix_value", bgpAFArg2) + if(value == "ok"): + command = command + bgpAFArg2 + " " + if(bgpAFArg3 is not None and bgpAFArg3 == "backdoor"): + command = command + bgpAFArg3 + elif(bgpAFArg3 is not None and bgpAFArg3 == "route-map"): + command = command + bgpAFArg3 + value = cnos.checkSanityofVariable( + deviceType, "addrfamily_routemap_name", bgpAFArg4) + if(value == "ok"): + command = command + bgpAFArg4 + " " + if(bgpAFArg5 is not None and + bgpAFArg5 == "backdoor"): + command = command + bgpAFArg5 + else: + retVal = "Error-298" + return retVal + else: + retVal = "Error-196" + return retVal + elif(bgpAFArg3 is not None and bgpAFArg3 == "mask"): + command = command + bgpAFArg3 + value = cnos.checkSanityofVariable( + deviceType, "network_ip_prefix_mask", bgpAFArg4) + if(value == "ok"): + command = command + bgpAFArg4 + " " + else: + retVal = "Error-299" + return retVal + else: + command = command.strip() + else: + retVal = "Error-300" + return retVal + + elif(bgpAFArg1 == "nexthop"): + command = command + bgpAFArg1 + " trigger-delay critical " + value = cnos.checkSanityofVariable( + deviceType, "nexthop_crtitical_delay", bgpAFArg2) + if(value == "ok"): + command = command + bgpAFArg2 + " " + value = cnos.checkSanityofVariable( + deviceType, "nexthop_noncrtitical_delay", bgpAFArg3) + if(value == "ok"): + command = command + bgpAFArg3 + " " + else: + retVal = "Error-198" + return retVal + else: + retVal = "Error-197" + return retVal + + elif(bgpAFArg1 == "redistribute"): + command = command + bgpAFArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "addrfamily_redistribute_option", bgpAFArg2) + if(value == "ok"): + if(bgpAFArg2 is not None): + command = command + bgpAFArg2 + " " + "route-map " + value = cnos.checkSanityofVariable( + deviceType, "addrfamily_routemap_name", bgpAFArg3) + if(value == "ok"): + command = command + bgpAFArg3 + else: + retVal = "Error-196" + return retVal + else: + retVal = "Error-195" + return retVal + + elif(bgpAFArg1 == "save" or bgpAFArg1 == "synchronization"): + command = command + bgpAFArg1 + + else: + retVal = "Error-194" + return retVal + # debugOutput(command) + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + command = "exit \n" + return retVal +# EOM + + +def bgpConfig(module, cmd, prompt, answer): + retVal = '' + command = '' + bgpArg1 = module.params['bgpArg1'] + bgpArg2 = module.params['bgpArg2'] + bgpArg3 = module.params['bgpArg3'] + bgpArg4 = module.params['bgpArg4'] + bgpArg5 = module.params['bgpArg5'] + bgpArg6 = module.params['bgpArg6'] + bgpArg7 = module.params['bgpArg7'] + bgpArg8 = module.params['bgpArg8'] + asNum = module.params['asNum'] + deviceType = module.params['deviceType'] + # cnos.debugOutput(bgpArg1) + if(bgpArg1 == "address-family"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "bgp_address_family", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + " " + "unicast \n" + # debugOutput(command) + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + bgpAFConfig(module, cmd, prompt, answer) + return retVal + else: + retVal = "Error-178" + return retVal + + elif(bgpArg1 == "bestpath"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + if(bgpArg2 == "always-compare-med"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + elif(bgpArg2 == "compare-confed-aspath"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + elif(bgpArg2 == "compare-routerid"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + elif(bgpArg2 == "dont-compare-originator-id"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + elif(bgpArg2 == "tie-break-on-age"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + elif(bgpArg2 == "as-path"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + " " + if(bgpArg3 == "ignore" or bgpArg3 == "multipath-relax"): + command = command + bgpArg3 + else: + retVal = "Error-179" + return retVal + elif(bgpArg2 == "med"): + # debugOutput(bgpArg2) + command = command + bgpArg2 + " " + if(bgpArg3 == "confed" or + bgpArg3 == "missing-as-worst" or + bgpArg3 == "non-deterministic" or + bgpArg3 == "remove-recv-med" or + bgpArg3 == "remove-send-med"): + command = command + bgpArg3 + else: + retVal = "Error-180" + return retVal + else: + retVal = "Error-181" + return retVal + + elif(bgpArg1 == "bgp"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " as-local-count " + value = cnos.checkSanityofVariable( + deviceType, "bgp_bgp_local_count", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + retVal = "Error-182" + return retVal + + elif(bgpArg1 == "cluster-id"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "cluster_id_as_ip", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + value = cnos.checkSanityofVariable( + deviceType, "cluster_id_as_number", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + retVal = "Error-183" + return retVal + + elif(bgpArg1 == "confederation"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + if(bgpArg2 == "identifier"): + value = cnos.checkSanityofVariable( + deviceType, "confederation_identifier", bgpArg3) + if(value == "ok"): + command = command + bgpArg2 + " " + bgpArg3 + "\n" + else: + retVal = "Error-184" + return retVal + elif(bgpArg2 == "peers"): + value = cnos.checkSanityofVariable( + deviceType, "confederation_peers_as", bgpArg3) + if(value == "ok"): + command = command + bgpArg2 + " " + bgpArg3 + else: + retVal = "Error-185" + return retVal + else: + retVal = "Error-186" + return retVal + + elif(bgpArg1 == "enforce-first-as"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + + elif(bgpArg1 == "fast-external-failover"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + + elif(bgpArg1 == "graceful-restart"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " stalepath-time " + value = cnos.checkSanityofVariable( + deviceType, "stalepath_delay_value", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + retVal = "Error-187" + return retVal + + elif(bgpArg1 == "graceful-restart-helper"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + + elif(bgpArg1 == "log-neighbor-changes"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + + elif(bgpArg1 == "maxas-limit"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "maxas_limit_as", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + retVal = "Error-188" + return retVal + + elif(bgpArg1 == "neighbor"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "neighbor_ipaddress", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + if(bgpArg3 is not None): + command = command + " remote-as " + value = cnos.checkSanityofVariable( + deviceType, "neighbor_as", bgpArg3) + if(value == "ok"): + # debugOutput(command) + command = command + bgpArg3 + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + bgpNeighborConfig(module, cmd, prompt, answer) + return retVal + else: + retVal = "Error-189" + return retVal + + elif(bgpArg1 == "router-id"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "router_id", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + retVal = "Error-190" + return retVal + + elif(bgpArg1 == "shutdown"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + + elif(bgpArg1 == "synchronization"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + + elif(bgpArg1 == "timers"): + # cnos.debugOutput(bgpArg3) + command = command + bgpArg1 + " bgp " + value = cnos.checkSanityofVariable( + deviceType, "bgp_keepalive_interval", bgpArg2) + if(value == "ok"): + command = command + bgpArg2 + else: + retVal = "Error-191" + return retVal + if(bgpArg3 is not None): + value = cnos.checkSanityofVariable(deviceType, "bgp_holdtime", bgpArg3) + if(value == "ok"): + command = command + " " + bgpArg3 + else: + retVal = "Error-192" + return retVal + else: + retVal = "Error-192" + return retVal + + elif(bgpArg1 == "vrf"): + # debugOutput(bgpArg1) + command = command + bgpArg1 + " default" + else: + # debugOutput(bgpArg1) + retVal = "Error-192" + return retVal + # debugOutput(command) + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + command = "exit \n" + # debugOutput(command) + return retVal +# EOM + + def main(): module = AnsibleModule( argument_spec=dict( @@ -407,54 +1233,19 @@ def main(): asNum=dict(required=True),), supports_check_mode=False) - username = module.params['username'] - password = module.params['password'] - enablePassword = module.params['enablePassword'] - bgpArg1 = module.params['bgpArg1'] - bgpArg2 = module.params['bgpArg2'] - bgpArg3 = module.params['bgpArg3'] - bgpArg4 = module.params['bgpArg4'] - bgpArg5 = module.params['bgpArg5'] - bgpArg6 = module.params['bgpArg6'] - bgpArg7 = module.params['bgpArg7'] - bgpArg8 = module.params['bgpArg8'] asNum = module.params['asNum'] outputfile = module.params['outputfile'] - hostIP = module.params['host'] deviceType = module.params['deviceType'] - output = "" - - if not HAS_PARAMIKO: - module.fail_json(msg='paramiko is required for this module') - # Create instance of SSHClient object - remote_conn_pre = paramiko.SSHClient() - - # Automatically add untrusted hosts (make sure okay for security policy in your environment) - remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - # initiate SSH connection with the switch - remote_conn_pre.connect(hostIP, username=username, password=password) - time.sleep(2) - - # Use invoke_shell to establish an 'interactive session' - remote_conn = remote_conn_pre.invoke_shell() - time.sleep(2) - - # Enable and enter configure terminal then send command - output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn) - - output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn) - - # Make terminal length = 0 - output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn) - - # Go to config mode - output = output + cnos.waitForDeviceResponse("configure device\n", "(config)#", 2, remote_conn) - - # Send the CLi command - output = output + cnos.routerConfig(remote_conn, deviceType, "(config)#", 2, "bgp", asNum, - bgpArg1, bgpArg2, bgpArg3, bgpArg4, bgpArg5, bgpArg6, bgpArg7, bgpArg8) - + output = '' + command = 'router bgp ' + value = cnos.checkSanityofVariable(deviceType, "bgp_as_number", asNum) + if(value == "ok"): + # BGP command happens here. It creates if not present + command = command + asNum + cmd = [{'command': command, 'prompt': None, 'answer': None}] + output = output + bgpConfig(module, cmd, '(config)#', None) + else: + output = "Error-176" # Save it into the file file = open(outputfile, "a") file.write(output) diff --git a/lib/ansible/modules/network/cnos/cnos_factory.py b/lib/ansible/modules/network/cnos/cnos_factory.py index 9b56bfd827..ee6a9f1ec5 100644 --- a/lib/ansible/modules/network/cnos/cnos_factory.py +++ b/lib/ansible/modules/network/cnos/cnos_factory.py @@ -33,22 +33,26 @@ DOCUMENTATION = ''' --- module: cnos_factory author: "Anil Kumar Muraleedharan (@amuraleedhar)" -short_description: Reset the switch's startup configuration to default (factory) on devices running Lenovo CNOS +short_description: Reset the switch startup configuration to default (factory) + on devices running Lenovo CNOS. description: - - This module allows you to reset a switch's startup configuration. The method provides a way to reset the - startup configuration to its factory settings. This is helpful when you want to move the switch to another - topology as a new network device. - This module uses SSH to manage network device configuration. - The results of the operation can be viewed in results directory. - For more information about this module from Lenovo and customizing it usage for your - use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_factory.html) + - This module allows you to reset a switch's startup configuration. The + method provides a way to reset the startup configuration to its factory + settings. This is helpful when you want to move the switch to another + topology as a new network device. This module uses SSH to manage network + device configuration. The result of the operation can be viewed in results + directory. + For more information about this module and customizing it usage + for your use cases, please visit + U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_factory.html) version_added: "2.3" extends_documentation_fragment: cnos options: {} ''' EXAMPLES = ''' -Tasks : The following are examples of using the module cnos_reload. These are written in the main.yml file of the tasks directory. +Tasks : The following are examples of using the module cnos_reload. These are + written in the main.yml file of the tasks directory. --- - name: Test Reset to factory cnos_factory: @@ -68,11 +72,6 @@ msg: ''' import sys -try: - import paramiko - HAS_PARAMIKO = True -except ImportError: - HAS_PARAMIKO = False import time import socket import array @@ -99,44 +98,11 @@ def main(): deviceType=dict(required=True),), supports_check_mode=False) - username = module.params['username'] - password = module.params['password'] - enablePassword = module.params['enablePassword'] - cliCommand = "save erase \n" + command = 'write erase' outputfile = module.params['outputfile'] - hostIP = module.params['host'] - deviceType = module.params['deviceType'] - output = "" - if not HAS_PARAMIKO: - module.fail_json(msg='paramiko is required for this module') - - # Create instance of SSHClient object - remote_conn_pre = paramiko.SSHClient() - - # Automatically add untrusted hosts (make sure okay for security policy in your environment) - remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - # initiate SSH connection with the switch - remote_conn_pre.connect(hostIP, username=username, password=password) - time.sleep(2) - - # Use invoke_shell to establish an 'interactive session' - remote_conn = remote_conn_pre.invoke_shell() - time.sleep(2) - - # Enable and enter configure terminal then send command - output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn) - - output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn) - - # Make terminal length = 0 - output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn) - - # cnos.debugOutput(cliCommand) - # Send the CLi command - output = output + cnos.waitForDeviceResponse(cliCommand, "[n]", 2, remote_conn) - - output = output + cnos.waitForDeviceResponse("y" + "\n", "#", 2, remote_conn) + output = '' + cmd = [{'command': command, 'prompt': '[n]', 'answer': 'y'}] + output = output + str(cnos.run_cnos_commands(module, cmd)) # Save it into the file file = open(outputfile, "a") @@ -145,7 +111,8 @@ def main(): errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): - module.exit_json(changed=True, msg="Switch Startup Config is Reset to factory settings ") + module.exit_json(changed=True, + msg="Switch Startup Config is Reset to Factory settings") else: module.fail_json(msg=errorMsg) diff --git a/lib/ansible/modules/network/cnos/cnos_reload.py b/lib/ansible/modules/network/cnos/cnos_reload.py index 23468cfd93..1e0319552a 100644 --- a/lib/ansible/modules/network/cnos/cnos_reload.py +++ b/lib/ansible/modules/network/cnos/cnos_reload.py @@ -35,19 +35,22 @@ module: cnos_reload author: "Anil Kumar Muraleedharan (@amuraleedhar)" short_description: Perform switch restart on devices running Lenovo CNOS description: - - This module allows you to restart the switch using the current startup configuration. - The module is usually invoked after the running configuration has been saved over the startup configuration. + - This module allows you to restart the switch using the current startup + configuration. The module is usually invoked after the running + configuration has been saved over the startup configuration. This module uses SSH to manage network device configuration. The results of the operation can be viewed in results directory. - For more information about this module from Lenovo and customizing it usage for your - use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_reload.html) + For more information about this module and customizing it usage + for your use cases, please visit + U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_reload.html) version_added: "2.3" extends_documentation_fragment: cnos options: {} ''' EXAMPLES = ''' -Tasks : The following are examples of using the module cnos_reload. These are written in the main.yml file of the tasks directory. +Tasks : The following are examples of using the module cnos_reload. These are + written in the main.yml file of the tasks directory. --- - name: Test Reload cnos_reload: @@ -67,11 +70,6 @@ msg: ''' import sys -try: - import paramiko - HAS_PARAMIKO = True -except ImportError: - HAS_PARAMIKO = False import time import socket import array @@ -98,44 +96,12 @@ def main(): deviceType=dict(required=True),), supports_check_mode=False) - username = module.params['username'] - password = module.params['password'] - enablePassword = module.params['enablePassword'] - cliCommand = "reload \n" + command = 'reload' outputfile = module.params['outputfile'] - hostIP = module.params['host'] - deviceType = module.params['deviceType'] - output = "" - if not HAS_PARAMIKO: - module.fail_json(msg='paramiko is required for this module') - - # Create instance of SSHClient object - remote_conn_pre = paramiko.SSHClient() - - # Automatically add untrusted hosts (make sure okay for security policy in your environment) - remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - # initiate SSH connection with the switch - remote_conn_pre.connect(hostIP, username=username, password=password) - time.sleep(2) - - # Use invoke_shell to establish an 'interactive session' - remote_conn = remote_conn_pre.invoke_shell() - time.sleep(2) - - # Enable and then send command - output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn) - - output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn) - - # Make terminal length = 0 - output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn) - - # Send the CLi command - output = output + cnos.waitForDeviceResponse(cliCommand, "(y/n):", 2, remote_conn) - - # Send the Confirmation y - output = output + cnos.waitForDeviceResponse("y\n", "#", 2, remote_conn) + output = '' + cmd = [{'command': command, 'prompt': 'reboot system? (y/n): ', + 'answer': 'y'}] + output = output + str(cnos.run_cnos_commands(module, cmd)) # Save it into the file file = open(outputfile, "a") @@ -144,7 +110,8 @@ def main(): errorMsg = cnos.checkOutputForError(output) if(errorMsg in "Device Response Timed out"): - module.exit_json(changed=True, msg="Device is Reloading. Please wait...") + module.exit_json(changed=True, + msg="Device is Reloading. Please wait...") else: module.fail_json(msg=errorMsg) diff --git a/lib/ansible/modules/network/cnos/cnos_save.py b/lib/ansible/modules/network/cnos/cnos_save.py index c612c0b16d..97e3c5deb1 100644 --- a/lib/ansible/modules/network/cnos/cnos_save.py +++ b/lib/ansible/modules/network/cnos/cnos_save.py @@ -33,22 +33,26 @@ DOCUMENTATION = ''' --- module: cnos_save author: "Anil Kumar Muraleedharan (@amuraleedhar)" -short_description: Save the running configuration as the startup configuration on devices running Lenovo CNOS +short_description: Save the running configuration as the startup configuration + on devices running Lenovo CNOS description: - - This module allows you to copy the running configuration of a switch over its startup configuration. - It is recommended to use this module shortly after any major configuration changes so they persist after - a switch restart. This module uses SSH to manage network device configuration. + - This module allows you to copy the running configuration of a switch over + its startup configuration. It is recommended to use this module shortly + after any major configuration changes so they persist after a switch + restart. This module uses SSH to manage network device configuration. The results of the operation will be placed in a directory named 'results' - that must be created by the user in their local directory to where the playbook is run. - For more information about this module from Lenovo and customizing it usage for your - use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_save.html) + that must be created by the user in their local directory to where the + playbook is run. For more information about this module from Lenovo and + customizing it usage for your use cases, please visit + U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_save.html) version_added: "2.3" extends_documentation_fragment: cnos options: {} ''' EXAMPLES = ''' -Tasks : The following are examples of using the module cnos_save. These are written in the main.yml file of the tasks directory. +Tasks : The following are examples of using the module cnos_save. These are + written in the main.yml file of the tasks directory. --- - name: Test Save cnos_save: @@ -68,11 +72,6 @@ msg: ''' import sys -try: - import paramiko - HAS_PARAMIKO = True -except ImportError: - HAS_PARAMIKO = False import time import socket import array @@ -99,42 +98,11 @@ def main(): deviceType=dict(required=True),), supports_check_mode=False) - username = module.params['username'] - password = module.params['password'] - enablePassword = module.params['enablePassword'] - cliCommand = "save memory \n" + command = 'write memory' outputfile = module.params['outputfile'] - hostIP = module.params['host'] - deviceType = module.params['deviceType'] - output = "" - if not HAS_PARAMIKO: - module.fail_json(msg='paramiko is required for this module') - - # Create instance of SSHClient object - remote_conn_pre = paramiko.SSHClient() - - # Automatically add untrusted hosts (make sure okay for security policy in your environment) - remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - # initiate SSH connection with the switch - remote_conn_pre.connect(hostIP, username=username, password=password) - time.sleep(2) - - # Use invoke_shell to establish an 'interactive session' - remote_conn = remote_conn_pre.invoke_shell() - time.sleep(2) - - # Enable and then send command - output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn) - - output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn) - - # Make terminal length = 0 - output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn) - - # cnos.debugOutput(cliCommand) - # Send the CLi command - output = output + cnos.waitForDeviceResponse(cliCommand, "#", 2, remote_conn) + output = '' + cmd = [{'command': command, 'prompt': None, 'answer': None}] + output = output + str(cnos.run_cnos_commands(module, cmd)) # Save it into the file file = open(outputfile, "a") @@ -143,7 +111,8 @@ def main(): errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): - module.exit_json(changed=True, msg="Switch Running Config is Saved to Startup Config ") + module.exit_json(changed=True, + msg="Switch Running Config is Saved to Startup Config ") else: module.fail_json(msg=errorMsg) diff --git a/lib/ansible/modules/network/cnos/cnos_showrun.py b/lib/ansible/modules/network/cnos/cnos_showrun.py index 787d01f68e..78bf5538f6 100644 --- a/lib/ansible/modules/network/cnos/cnos_showrun.py +++ b/lib/ansible/modules/network/cnos/cnos_showrun.py @@ -33,22 +33,25 @@ DOCUMENTATION = ''' --- module: cnos_showrun author: "Anil Kumar Muraleedharan (@amuraleedhar)" -short_description: Collect the current running configuration on devices running Lenovo CNOS +short_description: Collect the current running configuration on devices running on CNOS description: - - This module allows you to view the switch running configuration. It executes the display running-config CLI - command on a switch and returns a file containing the current running configuration of the target network + - This module allows you to view the switch running configuration. It + executes the display running-config CLI command on a switch and returns a + file containing the current running configuration of the target network device. This module uses SSH to manage network device configuration. The results of the operation will be placed in a directory named 'results' - that must be created by the user in their local directory to where the playbook is run. - For more information about this module from Lenovo and customizing it usage for your - use cases, please visit U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_showrun.html) + that must be created by the user in their local directory to where the + playbook is run. For more information about this module from Lenovo and + customizing it usage for your use cases, please visit + U(http://systemx.lenovofiles.com/help/index.jsp?topic=%2Fcom.lenovo.switchmgt.ansible.doc%2Fcnos_showrun.html) version_added: "2.3" extends_documentation_fragment: cnos options: {} ''' EXAMPLES = ''' -Tasks : The following are examples of using the module cnos_showrun. These are written in the main.yml file of the tasks directory. +Tasks : The following are examples of using the module cnos_showrun. These are + written in the main.yml file of the tasks directory. --- - name: Run show running-config cnos_showrun: @@ -69,11 +72,6 @@ msg: ''' import sys -try: - import paramiko - HAS_PARAMIKO = True -except ImportError: - HAS_PARAMIKO = False import time import socket import array @@ -99,41 +97,11 @@ def main(): enablePassword=dict(required=False, no_log=True),), supports_check_mode=False) - username = module.params['username'] - password = module.params['password'] - enablePassword = module.params['enablePassword'] - cliCommand = "display running-config" + command = 'show running-config' outputfile = module.params['outputfile'] - hostIP = module.params['host'] - output = "" - if not HAS_PARAMIKO: - module.fail_json(msg='paramiko is required for this module') - - # Create instance of SSHClient object - remote_conn_pre = paramiko.SSHClient() - - # Automatically add untrusted hosts (make sure okay for security policy in your environment) - remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - # initiate SSH connection with the switch - remote_conn_pre.connect(hostIP, username=username, password=password) - time.sleep(2) - - # Use invoke_shell to establish an 'interactive session' - remote_conn = remote_conn_pre.invoke_shell() - time.sleep(2) - - # Enable and then send command - output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn) - - output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn) - - # Make terminal length = 0 - output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn) - - # Send the CLi command - output = output + cnos.waitForDeviceResponse(cliCommand + "\n", "#", 2, remote_conn) - + output = '' + cmd = [{'command': command, 'prompt': None, 'answer': None}] + output = output + str(cnos.run_cnos_commands(module, cmd)) # Save it into the file file = open(outputfile, "a") file.write(output) @@ -141,7 +109,8 @@ def main(): errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): - module.exit_json(changed=True, msg="Running Configuration saved in file ") + module.exit_json(changed=True, + msg="Running Configuration saved in file ") else: module.fail_json(msg=errorMsg) diff --git a/test/units/modules/network/cnos/fixtures/cnos_bgp_config.cfg b/test/units/modules/network/cnos/fixtures/cnos_bgp_config.cfg new file mode 100644 index 0000000000..b625756635 --- /dev/null +++ b/test/units/modules/network/cnos/fixtures/cnos_bgp_config.cfg @@ -0,0 +1,24 @@ +! +router bgp 33 + router-id 1.2.3.4 + bestpath always-compare-med + cluster-id 1.2.3.4 + confederation identifier 333 + enforce-first-as + bgp as-local-count 33 + bestpath compare-confed-aspath + maxas-limit 333 + graceful-restart-helper + graceful-restart stalepath-time 333 + timers bgp 333 3333 + address-family ipv4 unicast + synchronization + network 0.0.0.0 backdoor + network 0.0.0.0 backdoor + dampening 13 233 333 15 33 + neighbor 10.241.107.40 remote-as 13 + bfd + address-family ipv4 unicast + next-hop-self +! + diff --git a/test/units/modules/network/cnos/test_cnos_bgp.py b/test/units/modules/network/cnos/test_cnos_bgp.py new file mode 100644 index 0000000000..1cff5e53f0 --- /dev/null +++ b/test/units/modules/network/cnos/test_cnos_bgp.py @@ -0,0 +1,99 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json +from ansible.compat.tests.mock import patch +from ansible.modules.network.cnos import cnos_bgp +from units.modules.utils import set_module_args +from .cnos_module import TestCnosModule, load_fixture + + +class TestCnosBgpModule(TestCnosModule): + + module = cnos_bgp + + def setUp(self): + super(TestCnosBgpModule, self).setUp() + + self.mock_run_cnos_commands = patch('ansible.module_utils.network.cnos.cnos.run_cnos_commands') + self.run_cnos_commands = self.mock_run_cnos_commands.start() + + def tearDown(self): + super(TestCnosBgpModule, self).tearDown() + self.mock_run_cnos_commands.stop() + + def load_fixtures(self, commands=None, transport='cli'): + self.run_cnos_commands.return_value = [load_fixture('cnos_bgp_config.cfg')] + + def test_bgp_neighbor(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'neighbor', 'bgpArg2': '10.241.107.40', + 'bgpArg3': '13', 'bgpArg4': 'address-family', + 'bgpArg5': 'ipv4', 'bgpArg6': 'next-hop-self'}) + result = self.execute_module(changed=True) + file = open('Anil.txt', "a") + file.write(str(result)) + file.close() + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result) + + def test_cnos_bgp_dampening(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'address-family', 'bgpArg2': 'ipv4', + 'bgpArg3': 'dampening', 'bgpArg4': '13', + 'bgpArg5': '233', 'bgpArg6': '333', + 'bgpArg7': '15', 'bgpArg8': '33'}) + result = self.execute_module(changed=True) + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result) + + def test_cnos_bgp_network(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'address-family', 'bgpArg2': 'ipv4', + 'bgpArg3': 'network', 'bgpArg4': '1.2.3.4/5', + 'bgpArg5': 'backdoor'}) + result = self.execute_module(changed=True) + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result) + + def test_cnos_bgp_clusterid(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'cluster-id', 'bgpArg2': '10.241.107.40'}) + result = self.execute_module(changed=True) + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result) + + def test_cnos_bgp_graceful_restart(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'graceful-restart', 'bgpArg2': '333'}) + result = self.execute_module(changed=True) + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result) + + def test_cnos_bgp_routerid(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'router-id', 'bgpArg2': '1.2.3.4'}) + result = self.execute_module(changed=True) + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result) + + def test_cnos_bgp_vrf(self): + set_module_args({'username': 'admin', 'password': 'pass', + 'host': '10.241.107.39', 'deviceType': 'g8272_cnos', + 'outputfile': 'test.log', 'asNum': '33', + 'bgpArg1': 'vrf'}) + result = self.execute_module(changed=True) + expected_result = 'BGP configurations accomplished' + self.assertEqual(result['msg'], expected_result)