示例#1
0
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

    # 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)
示例#2
0
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

    # 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)