From ceb49bdf1272d6b3e685b83b74d89b825176ab3c Mon Sep 17 00:00:00 2001 From: Anil Kumar Muraleedharan Date: Wed, 8 Aug 2018 23:54:39 +0530 Subject: [PATCH] Lenovo size reduce cnos.py of util (#43823) * Changing Lenovo Inc to Lenovo and update License file to be consistent. * Moving code in util file to module file. This is done as per a review comment * Update cnos_vlag.py * Update cnos_vlan.py * Update enos_config.py --- lib/ansible/module_utils/network/cnos/cnos.py | 517 ------------------ lib/ansible/modules/network/cnos/cnos_vlag.py | 214 +++++++- lib/ansible/modules/network/cnos/cnos_vlan.py | 386 ++++++++++++- 3 files changed, 566 insertions(+), 551 deletions(-) diff --git a/lib/ansible/module_utils/network/cnos/cnos.py b/lib/ansible/module_utils/network/cnos/cnos.py index a52e88375a..a1996c4e60 100644 --- a/lib/ansible/module_utils/network/cnos/cnos.py +++ b/lib/ansible/module_utils/network/cnos/cnos.py @@ -1363,523 +1363,6 @@ def portChannelConfig(module, prompt, answer): # EOM -def vlanConfig(module, prompt, answer): - - retVal = '' - # Wait time to get response from server - vlanArg1 = module.params['vlanArg1'] - vlanArg2 = module.params['vlanArg2'] - vlanArg3 = module.params['vlanArg3'] - vlanArg4 = module.params['vlanArg4'] - vlanArg5 = module.params['vlanArg5'] - deviceType = module.params['deviceType'] - # vlan config command happens here. - command = 'vlan ' - - if(vlanArg1 == "access-map"): - # debugOutput("access-map ") - command = command + vlanArg1 + ' ' - value = checkSanityofVariable( - deviceType, "vlan_access_map_name", vlanArg2) - if(value == "ok"): - command = command + vlanArg2 - # debugOutput(command) - cmd = [{'command': command, 'prompt': None, 'answer': None}] - retVal = retVal + vlanAccessMapConfig(module, cmd) - return retVal - else: - retVal = "Error-130" - return retVal - - elif(vlanArg1 == "dot1q"): - # debugOutput("dot1q") - command = command + vlanArg1 + " tag native " - if(vlanArg2 is not None): - value = checkSanityofVariable( - deviceType, "vlan_dot1q_tag", vlanArg2) - if(value == "ok"): - command = command + vlanArg2 - else: - retVal = "Error-131" - return retVal - - elif(vlanArg1 == "filter"): - # debugOutput( "filter") - command = command + vlanArg1 + " " - if(vlanArg2 is not None): - value = checkSanityofVariable( - deviceType, "vlan_filter_name", vlanArg2) - if(value == "ok"): - command = command + vlanArg2 + " vlan-list " - value = checkSanityofVariable(deviceType, "vlan_id", vlanArg3) - if(value == "ok"): - command = command + vlanArg3 - else: - value = checkSanityofVariable( - deviceType, "vlan_id_range", vlanArg3) - if(value == "ok"): - command = command + vlanArg3 - else: - retVal = "Error-133" - return retVal - else: - retVal = "Error-132" - return retVal - - else: - value = checkSanityofVariable(deviceType, "vlan_id", vlanArg1) - if(value == "ok"): - retVal = createVlan(module, '(config-vlan)#', None) - return retVal - else: - value = checkSanityofVariable( - deviceType, "vlan_id_range", vlanArg1) - if(value == "ok"): - retVal = createVlan(module, '(config-vlan)#', None) - return retVal - retVal = "Error-133" - return retVal - - # debugOutput(command) - cmd = [{'command': command, 'prompt': None, 'answer': None}] - retVal = retVal + str(run_cnos_commands(module, cmd)) - return retVal -# EOM - - -def vlanAccessMapConfig(module, cmd): - retVal = '' - # Wait time to get response from server - command = '' - vlanArg3 = module.params['vlanArg3'] - vlanArg4 = module.params['vlanArg4'] - vlanArg5 = module.params['vlanArg5'] - deviceType = module.params['deviceType'] - if(vlanArg3 == "action"): - command = command + vlanArg3 + ' ' - value = checkSanityofVariable( - deviceType, "vlan_accessmap_action", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-135" - return retVal - elif(vlanArg3 == "match"): - command = command + vlanArg3 + ' ' - if(vlanArg4 == "ip" or vlanArg4 == "mac"): - command = command + vlanArg4 + ' address ' - value = checkSanityofVariable( - deviceType, "vlan_access_map_name", vlanArg5) - if(value == "ok"): - command = command + vlanArg5 - else: - retVal = "Error-136" - return retVal - else: - retVal = "Error-137" - return retVal - elif(vlanArg3 == "statistics"): - command = vlanArg3 + " per-entry" - else: - retVal = "Error-138" - return retVal - - inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] - cmd.extend(inner_cmd) - retVal = retVal + str(run_cnos_commands(module, cmd)) - # debugOutput(command) - return retVal -# EOM - - -def checkVlanNameNotAssigned(module, prompt, answer): - retVal = "ok" - vlanId = module.params['vlanArg1'] - vlanName = module.params['vlanArg3'] - command = "show vlan id " + vlanId - cmd = [{'command': command, 'prompt': None, 'answer': None}] - retVal = str(run_cnos_commands(module, cmd)) - if(retVal.find('Error') != -1): - command = "display vlan id " + vlanId - retVal = str(run_cnos_commands(module, cmd)) - if(retVal.find(vlanName) != -1): - return "Nok" - else: - return "ok" -# EOM - - -# Utility Method to create vlan -def createVlan(module, prompt, answer): - - # vlan config command happens here. It creates if not present - vlanArg1 = module.params['vlanArg1'] - vlanArg2 = module.params['vlanArg2'] - vlanArg3 = module.params['vlanArg3'] - vlanArg4 = module.params['vlanArg4'] - vlanArg5 = module.params['vlanArg5'] - deviceType = module.params['deviceType'] - retVal = '' - command = 'vlan ' + vlanArg1 - # debugOutput(command) - cmd = [{'command': command, 'prompt': None, 'answer': None}] - command = "" - if(vlanArg2 == "name"): - # debugOutput("name") - command = vlanArg2 + " " - value = checkSanityofVariable(deviceType, "vlan_name", vlanArg3) - if(value == "ok"): - value = checkVlanNameNotAssigned(module, prompt, answer) - if(value == "ok"): - command = command + vlanArg3 - else: - retVal = retVal + 'VLAN Name is already assigned \n' - command = "\n" - else: - retVal = "Error-139" - return retVal - elif (vlanArg2 == "flood"): - # debugOutput("flood") - command = vlanArg2 + " " - value = checkSanityofVariable(deviceType, "vlan_flood", vlanArg3) - if(value == "ok"): - command = command + vlanArg3 - else: - retVal = "Error-140" - return retVal - - elif(vlanArg2 == "state"): - # debugOutput("state") - command = vlanArg2 + " " - value = checkSanityofVariable(deviceType, "vlan_state", vlanArg3) - if(value == "ok"): - command = command + vlanArg3 - else: - retVal = "Error-141" - return retVal - - elif(vlanArg2 == "ip"): - # debugOutput("ip") - command = vlanArg2 + " igmp snooping " - # debugOutput("vlanArg3") - if(vlanArg3 is None or vlanArg3 == ""): - # debugOutput("None or empty") - command = command.strip() - elif(vlanArg3 == "fast-leave"): - # debugOutput("fast-leave") - command = command + vlanArg3 - - elif (vlanArg3 == "last-member-query-interval"): - # debugOutput("last-member-query-interval") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_last_member_query_interval", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-142" - return retVal - - elif (vlanArg3 == "querier"): - # debugOutput("querier") - command = command + vlanArg3 + " " - value = checkSanityofVariable(deviceType, "vlan_querier", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-143" - return retVal - elif (vlanArg3 == "querier-timeout"): - # debugOutput("querier-timeout") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_querier_timeout", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-144" - return retVal - elif (vlanArg3 == "query-interval"): - # debugOutput("query-interval") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_query_interval", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-145" - return retVal - elif (vlanArg3 == "query-max-response-time"): - # debugOutput("query-max-response-time") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_query_max_response_time", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-146" - return retVal - elif (vlanArg3 == "report-suppression"): - # debugOutput("report-suppression") - command = command + vlanArg3 - - elif (vlanArg3 == "robustness-variable"): - # debugOutput("robustness-variable") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_robustness_variable", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-147" - return retVal - elif (vlanArg3 == "startup-query-count"): - # debugOutput("startup-query-count") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_startup_query_count", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-148" - return retVal - elif (vlanArg3 == "startup-query-interval"): - # debugOutput("startup-query-interval") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_startup_query_interval", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-149" - return retVal - elif (vlanArg3 == "static-group"): - retVal = "Error-102" - return retVal - elif (vlanArg3 == "version"): - # debugOutput("version") - command = command + vlanArg3 + " " - value = checkSanityofVariable( - deviceType, "vlan_snooping_version", vlanArg4) - if(value == "ok"): - command = command + vlanArg4 - else: - retVal = "Error-150" - return retVal - elif (vlanArg3 == "mrouter"): - # debugOutput("mrouter") - command = command + vlanArg3 + " interface " - if(vlanArg4 == "ethernet"): - command = command + vlanArg4 + " " - value = checkSanityofVariable( - deviceType, "vlan_ethernet_interface", vlanArg5) - if(value == "ok"): - command = command + vlanArg5 - else: - retVal = "Error-151" - return retVal - elif(vlanArg4 == "port-aggregation"): - command = command + vlanArg4 + " " - value = checkSanityofVariable( - deviceType, "vlan_portagg_number", vlanArg5) - if(value == "ok"): - command = command + vlanArg5 - else: - retVal = "Error-152" - return retVal - else: - retVal = "Error-153" - return retVal - else: - command = command + vlanArg3 - - else: - retVal = "Error-154" - return retVal - inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] - cmd.extend(inner_cmd) - retVal = retVal + str(run_cnos_commands(module, cmd)) - # debugOutput(command) - return retVal -# EOM - - -def vlagConfig(module, prompt, answer): - - retVal = '' - # vlag config command happens here. - command = 'vlag ' - - vlagArg1 = module.params['vlagArg1'] - vlagArg2 = module.params['vlagArg2'] - vlagArg3 = module.params['vlagArg3'] - vlagArg4 = module.params['vlagArg4'] - deviceType = module.params['deviceType'] - - if(vlagArg1 == "enable"): - # debugOutput("enable") - command = command + vlagArg1 + " " - - elif(vlagArg1 == "auto-recovery"): - # debugOutput("auto-recovery") - command = command + vlagArg1 + " " - value = checkSanityofVariable( - deviceType, "vlag_auto_recovery", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - else: - retVal = "Error-160" - return retVal - - elif(vlagArg1 == "config-consistency"): - # debugOutput("config-consistency") - command = command + vlagArg1 + " " - value = checkSanityofVariable( - deviceType, "vlag_config_consistency", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - else: - retVal = "Error-161" - return retVal - - elif(vlagArg1 == "isl"): - # debugOutput("isl") - command = command + vlagArg1 + " port-channel " - value = checkSanityofVariable( - deviceType, "vlag_port_aggregation", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - else: - retVal = "Error-162" - return retVal - - elif(vlagArg1 == "mac-address-table"): - # debugOutput("mac-address-table") - command = command + vlagArg1 + " refresh" - - elif(vlagArg1 == "peer-gateway"): - # debugOutput("peer-gateway") - command = command + vlagArg1 + " " - - elif(vlagArg1 == "priority"): - # debugOutput("priority") - command = command + vlagArg1 + " " - value = checkSanityofVariable(deviceType, "vlag_priority", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - else: - retVal = "Error-163" - return retVal - - elif(vlagArg1 == "startup-delay"): - # debugOutput("startup-delay") - command = command + vlagArg1 + " " - value = checkSanityofVariable( - deviceType, "vlag_startup_delay", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - else: - retVal = "Error-164" - return retVal - - elif(vlagArg1 == "tier-id"): - # debugOutput("tier-id") - command = command + vlagArg1 + " " - value = checkSanityofVariable(deviceType, "vlag_tier_id", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - else: - retVal = "Error-165" - return retVal - - elif(vlagArg1 == "vrrp"): - # debugOutput("vrrp") - command = command + vlagArg1 + " active" - - elif(vlagArg1 == "instance"): - # debugOutput("instance") - command = command + vlagArg1 + " " - value = checkSanityofVariable(deviceType, "vlag_instance", vlagArg2) - if(value == "ok"): - command = command + vlagArg2 - if(vlagArg3 is not None): - command = command + " port-channel " - value = checkSanityofVariable( - deviceType, "vlag_port_aggregation", vlagArg3) - if(value == "ok"): - command = command + vlagArg3 - else: - retVal = "Error-162" - return retVal - else: - command = command + " enable " - else: - retVal = "Error-166" - return retVal - - elif(vlagArg1 == "hlthchk"): - # debugOutput("hlthchk") - command = command + vlagArg1 + " " - value = checkSanityofVariable( - deviceType, "vlag_hlthchk_options", vlagArg2) - if(value == "ok"): - if(vlagArg2 == "keepalive-attempts"): - value = checkSanityofVariable( - deviceType, "vlag_keepalive_attempts", vlagArg3) - if(value == "ok"): - command = command + vlagArg2 + " " + vlagArg3 - else: - retVal = "Error-167" - return retVal - elif(vlagArg2 == "keepalive-interval"): - value = checkSanityofVariable( - deviceType, "vlag_keepalive_interval", vlagArg3) - if(value == "ok"): - command = command + vlagArg2 + " " + vlagArg3 - else: - retVal = "Error-168" - return retVal - elif(vlagArg2 == "retry-interval"): - value = checkSanityofVariable( - deviceType, "vlag_retry_interval", vlagArg3) - if(value == "ok"): - command = command + vlagArg2 + " " + vlagArg3 - else: - retVal = "Error-169" - return retVal - elif(vlagArg2 == "peer-ip"): - # Here I am not taking care of IPV6 option. - value = checkSanityofVariable( - deviceType, "vlag_peerip", vlagArg3) - if(value == "ok"): - command = command + vlagArg2 + " " + vlagArg3 - if(vlagArg4 is not None): - value = checkSanityofVariable( - deviceType, "vlag_peerip_vrf", vlagArg4) - if(value == "ok"): - command = command + " vrf " + vlagArg4 - else: - retVal = "Error-170" - return retVal - else: - retVal = "Error-171" - return retVal - - else: - retVal = "Error-172" - return retVal - - # debugOutput(command) - cmd = [{'command': command, 'prompt': None, 'answer': None}] - retVal = retVal + str(run_cnos_commands(module, cmd)) - return retVal -# EOM - -# Utility Method to back up the start up config -# This method supports only TFTP or FTP -# Tuning of timeout parameter is pending - - def doStartupConfigBackUp( protocol, timeout, confServerIp, confPath, confServerUser, confServerPwd, obj): diff --git a/lib/ansible/modules/network/cnos/cnos_vlag.py b/lib/ansible/modules/network/cnos/cnos_vlag.py index 0628fe9c7c..c7d34f11e6 100644 --- a/lib/ansible/modules/network/cnos/cnos_vlag.py +++ b/lib/ansible/modules/network/cnos/cnos_vlag.py @@ -33,7 +33,8 @@ DOCUMENTATION = ''' --- module: cnos_vlag author: "Anil Kumar Muraleedharan (@amuraleedhar)" -short_description: Manage VLAG resources and attributes on devices running Lenovo CNOS +short_description: Manage VLAG resources and attributes on devices running + Lenovo CNOS description: - This module allows you to work with virtual Link Aggregation Groups (vLAG) related configurations. The operators used are overloaded to ensure @@ -44,34 +45,44 @@ description: 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_vlag.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_vlag.html) version_added: "2.3" extends_documentation_fragment: cnos options: vlagArg1: description: - - This is an overloaded vlag first argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlag first argument. Usage of this argument can + be found is the User Guide referenced above. required: Yes default: Null - choices: [enable, auto-recovery,config-consistency,isl,mac-address-table,peer-gateway,priority,startup-delay,tier-id,vrrp,instance,hlthchk] + choices: [enable, auto-recovery,config-consistency,isl,mac-address-table, + peer-gateway,priority,startup-delay,tier-id,vrrp,instance,hlthchk] vlagArg2: description: - - This is an overloaded vlag second argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlag second argument. Usage of this argument can + be found is the User Guide referenced above. required: No default: Null - choices: [Interval in seconds,disable or strict,Port Aggregation Number,VLAG priority,Delay time in seconds,VLAG tier-id value, - VLAG instance number,keepalive-attempts,keepalive-interval,retry-interval,peer-ip] + choices: [Interval in seconds,disable or strict,Port Aggregation Number, + VLAG priority,Delay time in seconds,VLAG tier-id value, + VLAG instance number,keepalive-attempts,keepalive-interval, + retry-interval,peer-ip] vlagArg3: description: - - This is an overloaded vlag third argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlag third argument. Usage of this argument can + be found is the User Guide referenced above. required: No default: Null - choices: [enable or port-aggregation,Number of keepalive attempts,Interval in seconds,Interval in seconds,VLAG health check peer IP4 address] + choices: [enable or port-aggregation,Number of keepalive attempts, + Interval in seconds,Interval in seconds, + VLAG health check peer IP4 address] vlagArg4: description: - - This is an overloaded vlag fourth argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlag fourth argument. Usage of this argument can + be found is the User Guide referenced above. required: No default: Null choices: [Port Aggregation Number,default or management] @@ -79,7 +90,8 @@ options: ''' EXAMPLES = ''' -Tasks : The following are examples of using the module cnos_vlag. These are written in the main.yml file of the tasks directory. +Tasks : The following are examples of using the module cnos_vlag. These are + written in the main.yml file of the tasks directory. --- - name: Test Vlag - enable cnos_vlag: @@ -268,6 +280,180 @@ from ansible.module_utils.basic import AnsibleModule from collections import defaultdict +def vlagConfig(module, prompt, answer): + + retVal = '' + # vlag config command happens here. + command = 'vlag ' + + vlagArg1 = module.params['vlagArg1'] + vlagArg2 = module.params['vlagArg2'] + vlagArg3 = module.params['vlagArg3'] + vlagArg4 = module.params['vlagArg4'] + deviceType = module.params['deviceType'] + + if(vlagArg1 == "enable"): + # debugOutput("enable") + command = command + vlagArg1 + " " + + elif(vlagArg1 == "auto-recovery"): + # debugOutput("auto-recovery") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlag_auto_recovery", vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + else: + retVal = "Error-160" + return retVal + + elif(vlagArg1 == "config-consistency"): + # debugOutput("config-consistency") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlag_config_consistency", vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + else: + retVal = "Error-161" + return retVal + + elif(vlagArg1 == "isl"): + # debugOutput("isl") + command = command + vlagArg1 + " port-channel " + value = cnos.checkSanityofVariable( + deviceType, "vlag_port_aggregation", vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + else: + retVal = "Error-162" + return retVal + + elif(vlagArg1 == "mac-address-table"): + # debugOutput("mac-address-table") + command = command + vlagArg1 + " refresh" + + elif(vlagArg1 == "peer-gateway"): + # debugOutput("peer-gateway") + command = command + vlagArg1 + " " + + elif(vlagArg1 == "priority"): + # debugOutput("priority") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "vlag_priority", + vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + else: + retVal = "Error-163" + return retVal + + elif(vlagArg1 == "startup-delay"): + # debugOutput("startup-delay") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlag_startup_delay", vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + else: + retVal = "Error-164" + return retVal + + elif(vlagArg1 == "tier-id"): + # debugOutput("tier-id") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "vlag_tier_id", vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + else: + retVal = "Error-165" + return retVal + + elif(vlagArg1 == "vrrp"): + # debugOutput("vrrp") + command = command + vlagArg1 + " active" + + elif(vlagArg1 == "instance"): + # debugOutput("instance") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable(deviceType, "vlag_instance", + vlagArg2) + if(value == "ok"): + command = command + vlagArg2 + if(vlagArg3 is not None): + command = command + " port-channel " + value = cnos.checkSanityofVariable( + deviceType, "vlag_port_aggregation", vlagArg3) + if(value == "ok"): + command = command + vlagArg3 + else: + retVal = "Error-162" + return retVal + else: + command = command + " enable " + else: + retVal = "Error-166" + return retVal + + elif(vlagArg1 == "hlthchk"): + # debugOutput("hlthchk") + command = command + vlagArg1 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlag_hlthchk_options", vlagArg2) + if(value == "ok"): + if(vlagArg2 == "keepalive-attempts"): + value = cnos.checkSanityofVariable( + deviceType, "vlag_keepalive_attempts", vlagArg3) + if(value == "ok"): + command = command + vlagArg2 + " " + vlagArg3 + else: + retVal = "Error-167" + return retVal + elif(vlagArg2 == "keepalive-interval"): + value = cnos.checkSanityofVariable( + deviceType, "vlag_keepalive_interval", vlagArg3) + if(value == "ok"): + command = command + vlagArg2 + " " + vlagArg3 + else: + retVal = "Error-168" + return retVal + elif(vlagArg2 == "retry-interval"): + value = cnos.checkSanityofVariable( + deviceType, "vlag_retry_interval", vlagArg3) + if(value == "ok"): + command = command + vlagArg2 + " " + vlagArg3 + else: + retVal = "Error-169" + return retVal + elif(vlagArg2 == "peer-ip"): + # Here I am not taking care of IPV6 option. + value = cnos.checkSanityofVariable( + deviceType, "vlag_peerip", vlagArg3) + if(value == "ok"): + command = command + vlagArg2 + " " + vlagArg3 + if(vlagArg4 is not None): + value = cnos.checkSanityofVariable( + deviceType, "vlag_peerip_vrf", vlagArg4) + if(value == "ok"): + command = command + " vrf " + vlagArg4 + else: + retVal = "Error-170" + return retVal + else: + retVal = "Error-171" + return retVal + + else: + retVal = "Error-172" + return retVal + + # debugOutput(command) + cmd = [{'command': command, 'prompt': None, 'answer': None}] + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + return retVal +# EOM + + def main(): # # Define parameters for vlag creation entry @@ -290,7 +476,7 @@ def main(): output = "" # Send the CLi command - output = output + str(cnos.vlagConfig(module, '(config)#', None)) + output = output + str(vlagConfig(module, '(config)#', None)) # Save it into the file file = open(outputfile, "a") diff --git a/lib/ansible/modules/network/cnos/cnos_vlan.py b/lib/ansible/modules/network/cnos/cnos_vlan.py index 4d1fd6dc7f..258ad03d4a 100644 --- a/lib/ansible/modules/network/cnos/cnos_vlan.py +++ b/lib/ansible/modules/network/cnos/cnos_vlan.py @@ -34,7 +34,8 @@ DOCUMENTATION = ''' --- module: cnos_vlan author: "Anil Kumar Muraleedharan (@amuraleedhar)" -short_description: Manage VLAN resources and attributes on devices running Lenovo CNOS +short_description: Manage VLAN resources and attributes on devices running + Lenovo CNOS description: - This module allows you to work with VLAN related configurations. The operators used are overloaded to ensure control over switch VLAN @@ -43,45 +44,57 @@ description: filter. After passing this level, there are five VLAN arguments that will perform further configurations. They are vlanArg1, vlanArg2, vlanArg3, vlanArg4, and vlanArg5. The value of vlanArg1 will determine the way - following arguments will be evaluated. 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_vlan.html) + following arguments will be evaluated. 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_vlan.html) version_added: "2.3" extends_documentation_fragment: cnos options: vlanArg1: description: - - This is an overloaded vlan first argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlan first argument. Usage of this argument can + be found is the User Guide referenced above. required: true choices: [access-map, dot1q, filter, <1-3999> VLAN ID 1-3999 or range] vlanArg2: description: - - This is an overloaded vlan second argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlan second argument. Usage of this argument can + be found is the User Guide referenced above. choices: [VLAN Access Map name,egress-only,name, flood,state, ip] vlanArg3: description: - - This is an overloaded vlan third argument. Usage of this argument can be found is the User Guide referenced above. - choices: [action, match, statistics, enter VLAN id or range of vlan, ascii name for the VLAN, ipv4 or ipv6, active or suspend, fast-leave, - last-member-query-interval, mrouter, querier, querier-timeout, query-interval, query-max-response-time, report-suppression, - robustness-variable, startup-query-count, startup-query-interval, static-group] + - This is an overloaded vlan third argument. Usage of this argument can + be found is the User Guide referenced above. + choices: [action, match, statistics, enter VLAN id or range of vlan, + ascii name for the VLAN, ipv4 or ipv6, active or suspend, + fast-leave, last-member-query-interval, mrouter, querier, + querier-timeout, query-interval, query-max-response-time, + report-suppression, robustness-variable, startup-query-count, + startup-query-interval, static-group] vlanArg4: description: - - This is an overloaded vlan fourth argument. Usage of this argument can be found is the User Guide referenced above. - choices: [drop or forward or redirect, ip or mac,Interval in seconds,ethernet, port-aggregation, Querier IP address, - Querier Timeout in seconds, Query Interval in seconds, Query Max Response Time in seconds, Robustness Variable value, - Number of queries sent at startup, Query Interval at startup] + - This is an overloaded vlan fourth argument. Usage of this argument can + be found is the User Guide referenced above. + choices: [drop or forward or redirect, ip or mac,Interval in seconds, + ethernet, port-aggregation, Querier IP address, + Querier Timeout in seconds, Query Interval in seconds, + Query Max Response Time in seconds, Robustness Variable value, + Number of queries sent at startup, Query Interval at startup] vlanArg5: description: - - This is an overloaded vlan fifth argument. Usage of this argument can be found is the User Guide referenced above. + - This is an overloaded vlan fifth argument. Usage of this argument can + be found is the User Guide referenced above. choices: [access-list name, Slot/chassis number, Port Aggregation Number] ''' EXAMPLES = ''' -Tasks: The following are examples of using the module cnos_vlan. These are written in the main.yml file of the tasks directory. +Tasks: The following are examples of using the module cnos_vlan. These are + written in the main.yml file of the tasks directory. --- - name: Test Vlan - Create a vlan, name it cnos_vlan: @@ -196,6 +209,339 @@ from ansible.module_utils.basic import AnsibleModule from collections import defaultdict +def vlanAccessMapConfig(module, cmd): + retVal = '' + # Wait time to get response from server + command = '' + vlanArg3 = module.params['vlanArg3'] + vlanArg4 = module.params['vlanArg4'] + vlanArg5 = module.params['vlanArg5'] + deviceType = module.params['deviceType'] + if(vlanArg3 == "action"): + command = command + vlanArg3 + ' ' + value = cnos.checkSanityofVariable( + deviceType, "vlan_accessmap_action", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-135" + return retVal + elif(vlanArg3 == "match"): + command = command + vlanArg3 + ' ' + if(vlanArg4 == "ip" or vlanArg4 == "mac"): + command = command + vlanArg4 + ' address ' + value = cnos.checkSanityofVariable( + deviceType, "vlan_access_map_name", vlanArg5) + if(value == "ok"): + command = command + vlanArg5 + else: + retVal = "Error-136" + return retVal + else: + retVal = "Error-137" + return retVal + elif(vlanArg3 == "statistics"): + command = vlanArg3 + " per-entry" + else: + retVal = "Error-138" + return retVal + + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + # debugOutput(command) + return retVal +# EOM + + +def checkVlanNameNotAssigned(module, prompt, answer): + retVal = "ok" + vlanId = module.params['vlanArg1'] + vlanName = module.params['vlanArg3'] + command = "show vlan id " + vlanId + cmd = [{'command': command, 'prompt': None, 'answer': None}] + retVal = str(cnos.run_cnos_commands(module, cmd)) + if(retVal.find('Error') != -1): + command = "display vlan id " + vlanId + retVal = str(cnos.run_cnos_commands(module, cmd)) + if(retVal.find(vlanName) != -1): + return "Nok" + else: + return "ok" +# EOM + + +# Utility Method to create vlan +def createVlan(module, prompt, answer): + + # vlan config command happens here. It creates if not present + vlanArg1 = module.params['vlanArg1'] + vlanArg2 = module.params['vlanArg2'] + vlanArg3 = module.params['vlanArg3'] + vlanArg4 = module.params['vlanArg4'] + vlanArg5 = module.params['vlanArg5'] + deviceType = module.params['deviceType'] + retVal = '' + command = 'vlan ' + vlanArg1 + # debugOutput(command) + cmd = [{'command': command, 'prompt': None, 'answer': None}] + command = "" + if(vlanArg2 == "name"): + # debugOutput("name") + command = vlanArg2 + " " + value = cnos.checkSanityofVariable(deviceType, "vlan_name", vlanArg3) + if(value == "ok"): + value = checkVlanNameNotAssigned(module, prompt, answer) + if(value == "ok"): + command = command + vlanArg3 + else: + retVal = retVal + 'VLAN Name is already assigned \n' + command = "\n" + else: + retVal = "Error-139" + return retVal + elif (vlanArg2 == "flood"): + # debugOutput("flood") + command = vlanArg2 + " " + value = cnos.checkSanityofVariable(deviceType, "vlan_flood", vlanArg3) + if(value == "ok"): + command = command + vlanArg3 + else: + retVal = "Error-140" + return retVal + + elif(vlanArg2 == "state"): + # debugOutput("state") + command = vlanArg2 + " " + value = cnos.checkSanityofVariable(deviceType, "vlan_state", vlanArg3) + if(value == "ok"): + command = command + vlanArg3 + else: + retVal = "Error-141" + return retVal + + elif(vlanArg2 == "ip"): + # debugOutput("ip") + command = vlanArg2 + " igmp snooping " + # debugOutput("vlanArg3") + if(vlanArg3 is None or vlanArg3 == ""): + # debugOutput("None or empty") + command = command.strip() + elif(vlanArg3 == "fast-leave"): + # debugOutput("fast-leave") + command = command + vlanArg3 + + elif (vlanArg3 == "last-member-query-interval"): + # debugOutput("last-member-query-interval") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_last_member_query_interval", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-142" + return retVal + + elif (vlanArg3 == "querier"): + # debugOutput("querier") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable(deviceType, + "vlan_querier", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-143" + return retVal + elif (vlanArg3 == "querier-timeout"): + # debugOutput("querier-timeout") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_querier_timeout", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-144" + return retVal + elif (vlanArg3 == "query-interval"): + # debugOutput("query-interval") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_query_interval", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-145" + return retVal + elif (vlanArg3 == "query-max-response-time"): + # debugOutput("query-max-response-time") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_query_max_response_time", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-146" + return retVal + elif (vlanArg3 == "report-suppression"): + # debugOutput("report-suppression") + command = command + vlanArg3 + + elif (vlanArg3 == "robustness-variable"): + # debugOutput("robustness-variable") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_startup_query_count", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-148" + return retVal + elif (vlanArg3 == "startup-query-interval"): + # debugOutput("startup-query-interval") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_startup_query_interval", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-149" + return retVal + elif (vlanArg3 == "static-group"): + retVal = "Error-102" + return retVal + elif (vlanArg3 == "version"): + # debugOutput("version") + command = command + vlanArg3 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_snooping_version", vlanArg4) + if(value == "ok"): + command = command + vlanArg4 + else: + retVal = "Error-150" + return retVal + elif (vlanArg3 == "mrouter"): + # debugOutput("mrouter") + command = command + vlanArg3 + " interface " + if(vlanArg4 == "ethernet"): + command = command + vlanArg4 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_ethernet_interface", vlanArg5) + if(value == "ok"): + command = command + vlanArg5 + else: + retVal = "Error-151" + return retVal + elif(vlanArg4 == "port-aggregation"): + command = command + vlanArg4 + " " + value = cnos.checkSanityofVariable( + deviceType, "vlan_portagg_number", vlanArg5) + if(value == "ok"): + command = command + vlanArg5 + else: + retVal = "Error-152" + return retVal + else: + retVal = "Error-153" + return retVal + else: + command = command + vlanArg3 + + else: + retVal = "Error-154" + return retVal + inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] + cmd.extend(inner_cmd) + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + # debugOutput(command) + return retVal +# EOM + + +def vlanConfig(module, prompt, answer): + + retVal = '' + # Wait time to get response from server + vlanArg1 = module.params['vlanArg1'] + vlanArg2 = module.params['vlanArg2'] + vlanArg3 = module.params['vlanArg3'] + vlanArg4 = module.params['vlanArg4'] + vlanArg5 = module.params['vlanArg5'] + deviceType = module.params['deviceType'] + # vlan config command happens here. + command = 'vlan ' + + if(vlanArg1 == "access-map"): + # debugOutput("access-map ") + command = command + vlanArg1 + ' ' + value = cnos.checkSanityofVariable( + deviceType, "vlan_access_map_name", vlanArg2) + if(value == "ok"): + command = command + vlanArg2 + # debugOutput(command) + cmd = [{'command': command, 'prompt': None, 'answer': None}] + retVal = retVal + vlanAccessMapConfig(module, cmd) + return retVal + else: + retVal = "Error-130" + return retVal + + elif(vlanArg1 == "dot1q"): + # debugOutput("dot1q") + command = command + vlanArg1 + " tag native " + if(vlanArg2 is not None): + value = cnos.checkSanityofVariable( + deviceType, "vlan_dot1q_tag", vlanArg2) + if(value == "ok"): + command = command + vlanArg2 + else: + retVal = "Error-131" + return retVal + + elif(vlanArg1 == "filter"): + # debugOutput( "filter") + command = command + vlanArg1 + " " + if(vlanArg2 is not None): + value = cnos.checkSanityofVariable( + deviceType, "vlan_filter_name", vlanArg2) + if(value == "ok"): + command = command + vlanArg2 + " vlan-list " + value = cnos.checkSanityofVariable(deviceType, "vlan_id", + vlanArg3) + if(value == "ok"): + command = command + vlanArg3 + else: + value = cnos.checkSanityofVariable( + deviceType, "vlan_id_range", vlanArg3) + if(value == "ok"): + command = command + vlanArg3 + else: + retVal = "Error-133" + return retVal + else: + retVal = "Error-132" + return retVal + + else: + value = cnos.checkSanityofVariable(deviceType, "vlan_id", vlanArg1) + if(value == "ok"): + retVal = createVlan(module, '(config-vlan)#', None) + return retVal + else: + value = cnos.checkSanityofVariable( + deviceType, "vlan_id_range", vlanArg1) + if(value == "ok"): + retVal = createVlan(module, '(config-vlan)#', None) + return retVal + retVal = "Error-133" + return retVal + + # debugOutput(command) + cmd = [{'command': command, 'prompt': None, 'answer': None}] + retVal = retVal + str(cnos.run_cnos_commands(module, cmd)) + return retVal +# EOM + + def main(): # # Define parameters for vlan creation entry @@ -220,7 +566,7 @@ def main(): output = "" # Send the CLi command - output = output + str(cnos.vlanConfig(module, "(config)#", None)) + output = output + str(vlanConfig(module, "(config)#", None)) # Save it operation details into the file file = open(outputfile, "a")