def main(): module = AnsibleModule(argument_spec=dict( outputfile=dict(required=True), host=dict(required=False), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), ), supports_check_mode=False) command = 'show running-config' outputfile = module.params['outputfile'] 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) file.close() errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="Running Configuration saved in file ") else: module.fail_json(msg=errorMsg)
def main(): # # Define parameters for vlag creation entry # module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), vlagArg1=dict(required=True), vlagArg2=dict(required=False), vlagArg3=dict(required=False), vlagArg4=dict(required=False),), supports_check_mode=False) outputfile = module.params['outputfile'] output = "" # Send the CLi command output = output + str(cnos.vlagConfig(module, '(config)#', None)) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # need to add logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="VLAG configurations accomplished") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] cliCommand = "save erase \n" 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) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="Switch Startup Config is Reset to factory settings ") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( outputfile=dict(required=True), host=dict(required=False), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), ), supports_check_mode=False) command = 'write erase' outputfile = module.params['outputfile'] 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") file.write(output) file.close() errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json( changed=True, msg="Switch Startup Config is Reset to Factory settings") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=False), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True),), supports_check_mode=False) command = 'reload' outputfile = module.params['outputfile'] 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") file.write(output) file.close() errorMsg = cnos.checkOutputForError(output) if(errorMsg in "Device Response Timed out"): module.exit_json(changed=True, msg="Device is Reloading. Please wait...") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] cliCommand = "reload \n" 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) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() errorMsg = cnos.checkOutputForError(output) if(errorMsg in "Device Response Timed out"): module.exit_json(changed=True, msg="Device is Reloading. Please wait...") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( outputfile=dict(required=True), host=dict(required=False), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), interfaceRange=dict(required=False), interfaceOption=dict(required=False), interfaceArg1=dict(required=True), interfaceArg2=dict(required=False), interfaceArg3=dict(required=False), interfaceArg4=dict(required=False), interfaceArg5=dict(required=False), interfaceArg6=dict(required=False), interfaceArg7=dict(required=False), ), supports_check_mode=False) interfaceOption = module.params['interfaceOption'] outputfile = module.params['outputfile'] output = '' # Send the CLi command if (interfaceOption is None or interfaceOption == ""): output = output + cnos.interfaceConfig(module, "(config)#", None, None) elif (interfaceOption == "ethernet"): output = output + cnos.interfaceConfig(module, "(config)#", "ethernet", None) elif (interfaceOption == "loopback"): output = output + cnos.interfaceConfig(module, "(config)#", "loopback", None) elif (interfaceOption == "mgmt"): output = output + cnos.interfaceConfig(module, "(config)#", "mgmt", None) elif (interfaceOption == "port-channel"): output = output + cnos.interfaceConfig(module, "(config)#", "port-channel", None) elif (interfaceOption == "vlan"): output = output + cnos.interfaceConfig(module, "(config)#", "vlan", None) else: output = "Invalid interface option \n" # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="Interface Configuration is Accomplished") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( commandfile=dict(required=True), outputfile=dict(required=True), condition=dict(required=True), flag=dict(required=True), host=dict(required=False), deviceType=dict(required=True), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), ), supports_check_mode=False) condition = module.params['condition'] flag = module.params['flag'] commandfile = module.params['commandfile'] outputfile = module.params['outputfile'] output = '' if (condition is None or condition != flag): module.exit_json(changed=True, msg="Template Skipped for this switch") return " " # Send commands one by one f = open(commandfile, "r") cmd = [] for line in f: # Omit the comment lines in template file if not line.startswith("#"): # cnos.debugOutput(line) command = line.strip() inner_cmd = [{'command': command, 'prompt': None, 'answer': None}] cmd.extend(inner_cmd) # Write to memory save_cmd = [{'command': 'save', 'prompt': None, 'answer': None}] cmd.extend(save_cmd) output = output + str(cnos.run_cnos_commands(module, cmd)) # Write output to file path = outputfile.rsplit('/', 1) # cnos.debugOutput(path[0]) if not os.path.exists(path[0]): os.makedirs(path[0]) file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="Template Applied") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( clicommand=dict(required=True), outputfile=dict(required=True), condition=dict(required=True), flag=dict(required=True), host=dict(required=False), deviceType=dict(required=True), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), ), supports_check_mode=False) condition = module.params['condition'] flag = module.params['flag'] cliCommand = module.params['clicommand'] outputfile = module.params['outputfile'] output = '' if (condition is None or condition != flag): module.exit_json(changed=True, msg="Command Skipped for this switch") return '' # Send the CLi command cmd = [{'command': cliCommand, 'prompt': None, 'answer': None}] output = output + str(cnos.run_cnos_commands(module, cmd)) # Write to memory save_cmd = [{'command': 'save', 'prompt': None, 'answer': None}] cmd.extend(save_cmd) output = output + str(cnos.run_cnos_commands(module, cmd)) # Save it into the file path = outputfile.rsplit('/', 1) # cnos.debugOutput(path[0]) if not os.path.exists(path[0]): os.makedirs(path[0]) file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="CLI Command executed and results saved in file ") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), bgpArg1=dict(required=True), bgpArg2=dict(required=False), bgpArg3=dict(required=False), bgpArg4=dict(required=False), bgpArg5=dict(required=False), bgpArg6=dict(required=False), bgpArg7=dict(required=False), bgpArg8=dict(required=False), asNum=dict(required=True),), supports_check_mode=False) asNum = module.params['asNum'] outputfile = module.params['outputfile'] deviceType = module.params['deviceType'] 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) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="BGP configurations accomplished") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( outputfile=dict(required=True), host=dict(required=False), username=dict(required=False), password=dict(required=False, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), configType=dict(required=True), protocol=dict(required=True), serverip=dict(required=True), rcpath=dict(required=True), serverusername=dict(required=False), serverpassword=dict(required=False, no_log=True), ), supports_check_mode=False) outputfile = module.params['outputfile'] protocol = module.params['protocol'].lower() output = '' if (protocol == "tftp" or protocol == "ftp" or protocol == "sftp" or protocol == "scp"): transfer_status = doConfigBackUp(module, None, None) else: transfer_status = "Invalid Protocol option" output = output + "\n Config Back Up status \n" + transfer_status # Save it into the file path = outputfile.rsplit('/', 1) # cnos.debugOutput(path[0]) if not os.path.exists(path[0]): os.makedirs(path[0]) file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="Config file tranferred to server") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), interfaceRange=dict(required=False), interfaceArg1=dict(required=True), interfaceArg2=dict(required=False), interfaceArg3=dict(required=False), interfaceArg4=dict(required=False), interfaceArg5=dict(required=False), interfaceArg6=dict(required=False), interfaceArg7=dict(required=False), ), supports_check_mode=False) outputfile = module.params['outputfile'] output = '' # Send the CLi command if (module.params['interfaceArg1'] == "port-channel"): output = output + cnos.portChannelConfig(module, "(config)#", None) else: output = output + cnos.interfaceConfig(module, "(config)#", "port-channel", None) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="Port Channel Configuration is done") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), interfaceRange=dict(required=False), interfaceOption=dict(required=False), interfaceArg1=dict(required=True), interfaceArg2=dict(required=False), interfaceArg3=dict(required=False), interfaceArg4=dict(required=False), interfaceArg5=dict(required=False), interfaceArg6=dict(required=False), interfaceArg7=dict(required=False),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] interfaceRange = module.params['interfaceRange'] interfaceOption = module.params['interfaceOption'] interfaceArg1 = module.params['interfaceArg1'] interfaceArg2 = module.params['interfaceArg2'] interfaceArg3 = module.params['interfaceArg3'] interfaceArg4 = module.params['interfaceArg4'] interfaceArg5 = module.params['interfaceArg5'] interfaceArg6 = module.params['interfaceArg6'] interfaceArg7 = module.params['interfaceArg7'] 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 d\n", "(config)#", 2, remote_conn) # Send the CLi command if(interfaceOption is None or interfaceOption == ""): output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, None, interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif(interfaceOption == "ethernet"): output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "ethernet", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif(interfaceOption == "loopback"): output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "loopback", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif(interfaceOption == "mgmt"): output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "mgmt", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif(interfaceOption == "port-aggregation"): output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "port-aggregation", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif(interfaceOption == "vlan"): output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "vlan", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) else: output = "Invalid interface option \n" # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="Interface Configuration is done") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( clicommand=dict(required=True), outputfile=dict(required=True), condition=dict(required=True), flag=dict(required=True), host=dict(required=True), deviceType=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), ), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] condition = module.params['condition'] flag = module.params['flag'] cliCommand = module.params['clicommand'] outputfile = module.params['outputfile'] deviceType = module.params['deviceType'] hostIP = module.params['host'] output = "" if not HAS_PARAMIKO: module.fail_json(msg='paramiko is required for this module') if (condition != flag): module.exit_json(changed=True, msg="Command Skipped for this value") return " " # 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 # # 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 d\n", "(config)#", 2, remote_conn) # Send the CLi command output = output + cnos.waitForDeviceResponse(cliCommand + "\n", "(config)#", 2, remote_conn) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="CLI Command executed and results saved in file ") else: module.fail_json(msg=errorMsg)
def main(): # # Define parameters for vlan creation entry # module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), vlanArg1=dict(required=True), vlanArg2=dict(required=False), vlanArg3=dict(required=False), vlanArg4=dict(required=False), vlanArg5=dict(required=False),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] vlanArg1 = module.params['vlanArg1'] vlanArg2 = module.params['vlanArg2'] vlanArg3 = module.params['vlanArg3'] vlanArg4 = module.params['vlanArg4'] vlanArg5 = module.params['vlanArg5'] 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("conf d\n", "(config)#", 2, remote_conn) # Send the CLi command output = output + \ cnos.vlanConfig( remote_conn, deviceType, "(config)#", 2, vlanArg1, vlanArg2, vlanArg3, vlanArg4, vlanArg5) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # need to add logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="VLAN configuration is accomplished") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), interfaceRange=dict(required=False), interfaceOption=dict(required=False), interfaceArg1=dict(required=True), interfaceArg2=dict(required=False), interfaceArg3=dict(required=False), interfaceArg4=dict(required=False), interfaceArg5=dict(required=False), interfaceArg6=dict(required=False), interfaceArg7=dict(required=False), ), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] interfaceRange = module.params['interfaceRange'] interfaceOption = module.params['interfaceOption'] interfaceArg1 = module.params['interfaceArg1'] interfaceArg2 = module.params['interfaceArg2'] interfaceArg3 = module.params['interfaceArg3'] interfaceArg4 = module.params['interfaceArg4'] interfaceArg5 = module.params['interfaceArg5'] interfaceArg6 = module.params['interfaceArg6'] interfaceArg7 = module.params['interfaceArg7'] 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 d\n", "(config)#", 2, remote_conn) # Send the CLi command if (interfaceOption is None or interfaceOption == ""): output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, None, interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif (interfaceOption == "ethernet"): output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, "ethernet", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif (interfaceOption == "loopback"): output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, "loopback", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif (interfaceOption == "mgmt"): output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, "mgmt", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif (interfaceOption == "port-aggregation"): output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, "port-aggregation", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) elif (interfaceOption == "vlan"): output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, "vlan", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) else: output = "Invalid interface option \n" # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="Interface Configuration is done") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), protocol=dict(required=True), serverip=dict(required=True), imgpath=dict(required=True), imgtype=dict(required=True), serverusername=dict(required=False), serverpassword=dict(required=False, no_log=True),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] outputfile = module.params['outputfile'] host = module.params['host'] deviceType = module.params['deviceType'] protocol = module.params['protocol'].lower() imgserverip = module.params['serverip'] imgpath = module.params['imgpath'] imgtype = module.params['imgtype'] imgserveruser = module.params['serverusername'] imgserverpwd = module.params['serverpassword'] output = "" timeout = 120 tftptimeout = 600 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(host, 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) transfer_status = "" # Invoke method for image transfer from server if(protocol == "tftp" or protocol == "ftp"): transfer_status = cnos.doImageTransfer(protocol, tftptimeout, imgserverip, imgpath, imgtype, imgserveruser, imgserverpwd, remote_conn) elif(protocol == "sftp" or protocol == "scp"): transfer_status = cnos.doSecureImageTransfer(protocol, timeout, imgserverip, imgpath, imgtype, imgserveruser, imgserverpwd, remote_conn) else: transfer_status = "Invalid Protocol option" output = output + "\n Image Transfer status \n" + transfer_status # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="Image file tranferred to device") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule(argument_spec=dict( clicommand=dict(required=True), outputfile=dict(required=True), condition=dict(required=True), flag=dict(required=True), host=dict(required=True), deviceType=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), ), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] condition = module.params['condition'] flag = module.params['flag'] cliCommand = module.params['clicommand'] outputfile = module.params['outputfile'] deviceType = module.params['deviceType'] hostIP = module.params['host'] output = "" if not HAS_PARAMIKO: module.fail_json(msg='paramiko is required for this module') if (condition != flag): module.exit_json(changed=True, msg="Command Skipped for this value") return " " # 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 # # 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.waitForDeviceResponse(cliCommand + "\n", "(config)#", 2, remote_conn) # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if (errorMsg is None): module.exit_json(changed=True, msg="CLI Command executed and results saved in file ") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( outputfile=dict(required=True), host=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True), deviceType=dict(required=True), protocol=dict(required=True), serverip=dict(required=True), imgpath=dict(required=True), imgtype=dict(required=True), serverusername=dict(required=False), serverpassword=dict(required=False, no_log=True),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] outputfile = module.params['outputfile'] host = module.params['host'] deviceType = module.params['deviceType'] protocol = module.params['protocol'].lower() imgserverip = module.params['serverip'] imgpath = module.params['imgpath'] imgtype = module.params['imgtype'] imgserveruser = module.params['serverusername'] imgserverpwd = module.params['serverpassword'] output = "" timeout = 120 tftptimeout = 600 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(host, 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) transfer_status = "" # Invoke method for image transfer from server if(protocol == "tftp" or protocol == "ftp"): transfer_status = cnos.doImageTransfer(protocol, tftptimeout, imgserverip, imgpath, imgtype, imgserveruser, imgserverpwd, remote_conn) elif(protocol == "sftp" or protocol == "scp"): transfer_status = cnos.doSecureImageTransfer(protocol, timeout, imgserverip, imgpath, imgtype, imgserveruser, imgserverpwd, remote_conn) else: transfer_status = "Invalid Protocol option" output = output + "\n Image Transfer status \n" + transfer_status # Save it into the file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="Image file tranferred to device") else: module.fail_json(msg=errorMsg)
def main(): module = AnsibleModule( argument_spec=dict( commandfile=dict(required=True), outputfile=dict(required=True), host=dict(required=True), deviceType=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), enablePassword=dict(required=False, no_log=True),), supports_check_mode=False) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] commandfile = module.params['commandfile'] outputfile = module.params['outputfile'] deviceType = module.params['deviceType'] 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 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 d\n", "(config)#", 2, remote_conn) # Send commands one by one to the device # with open(commandfile, "r") as f: f = open(commandfile, "r") for line in f: # Omit the comment lines in template file if not line.startswith("#"): command = line if not line.endswith("\n"): command = command + "\n" response = cnos.waitForDeviceResponse(command, "#", 2, remote_conn) errorMsg = cnos.checkOutputForError(response) output = output + response if(errorMsg is not None): break # To cater to Mufti case # Write to memory output = output + cnos.waitForDeviceResponse("save\n", "#", 3, remote_conn) # Write output to file file = open(outputfile, "a") file.write(output) file.close() # Logic to check when changes occur or not errorMsg = cnos.checkOutputForError(output) if(errorMsg is None): module.exit_json(changed=True, msg="Template Applied") else: module.fail_json(msg=errorMsg)