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) username = module.params['username'] password = module.params['password'] enablePassword = module.params['enablePassword'] interfaceRange = module.params['interfaceRange'] 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 = "" # 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(interfaceArg1 == "port-aggregation"): output = output + cnos.portChannelConfig(remote_conn, deviceType, "(config)#", 2, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) else: output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "port-aggregation", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) # 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 Aggregation 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), 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'] 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 = "" # 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 (interfaceArg1 == "port-aggregation"): output = output + cnos.portChannelConfig( remote_conn, deviceType, "(config)#", 2, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) else: output = output + cnos.interfaceConfig( remote_conn, deviceType, "(config)#", 2, "port-aggregation", interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7) # 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 Aggregation configuration is done") else: module.fail_json(msg=errorMsg)