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) 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 = "" # 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 output = output + cnos.routerConfig( remote_conn, deviceType, "(config)#", 2, "bgp", asNum, bgpArg1, bgpArg2, bgpArg3, bgpArg4, bgpArg5, bgpArg6, bgpArg7, bgpArg8) # 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=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) 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 = "" # 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 output = output + cnos.routerConfig(remote_conn, deviceType, "(config)#", 2, "bgp", asNum, bgpArg1, bgpArg2, bgpArg3, bgpArg4, bgpArg5, bgpArg6, bgpArg7, bgpArg8) # 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)