# Buggy code
def transfer_file(module, dest):
    file_size = os.path.getsize(module.params["local_file"])

    if not enough_space(module):
        module.fail_json(msg="Could not transfer file. Not enough space on device.")

    hostname = module.params["host"]
    username = module.params["username"]
    password = module.params["password"]

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, username=username, password=password)

    full_remote_path = "{}{}".format(module.params["file_system"], dest)
    scp = SCPClient(ssh.get_transport())
    try:
        scp.put(module.params["local_file"], full_remote_path)
    except:
        time.sleep(10)
        temp_size = verify_remote_file_exists(
            module, dest, file_system=module.params["file_system"]
        )
        if int(temp_size) == int(file_size):
            pass
        else:
            module.fail_json(
                msg="Could not transfer file. There was an error "
                "during transfer. Please make sure remote "
                "permissions are set.",
                temp_size=temp_size,
                file_size=file_size,
            )
    scp.close()
    ssh.close()
    return True


# Traceback (socket.error)
Traceback (most recent call last):
File "/tmp/ansible_FP97GU/ansible_module_nxos_file_copy.py", line 230, in <module>
main()
File "/tmp/ansible_FP97GU/ansible_module_nxos_file_copy.py", line 220, in main
transfer_file(module, dest)
File "/tmp/ansible_FP97GU/ansible_module_nxos_file_copy.py", line 156, in transfer_file
password=password)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 310, in connect
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib/python2.7/site-packages/paramiko/util.py", line 276, in retry_on_signal
return function()
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 310, in <lambda>
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Address not available

fatal: [172.20.157.12]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_FP97GU/ansible_module_nxos_file_copy.py\", line 230, in <module>\n    main()\n  File \"/tmp/ansible_FP97GU/ansible_module_nxos_file_copy.py\", line 220, in main\n    transfer_file(module, dest)\n  File \"/tmp/ansible_FP97GU/ansible_module_nxos_file_copy.py\", line 156, in transfer_file\n    password=password)\n  File \"/usr/lib/python2.7/site-packages/paramiko/client.py\", line 310, in connect\n    retry_on_signal(lambda: sock.connect(addr))\n  File \"/usr/lib/python2.7/site-packages/paramiko/util.py\", line 276, in retry_on_signal\n    return function()\n  File \"/usr/lib/python2.7/site-packages/paramiko/client.py\", line 310, in <lambda>\n    retry_on_signal(lambda: sock.connect(addr))\n  File \"/usr/lib/python2.7/socket.py\", line 228, in meth\n    return getattr(self._sock,name)(*args)\nsocket.error: [Errno 99] Address not available\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def transfer_file(module, dest):
    file_size = os.path.getsize(module.params["local_file"])

    if not enough_space(module):
        module.fail_json(msg="Could not transfer file. Not enough space on device.")

    provider = module.params["provider"]

    hostname = module.params.get("host") or provider.get("host")
    username = module.params.get("username") or provider.get("username")
    password = module.params.get("password") or provider.get("password")

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, username=username, password=password)

    full_remote_path = "{}{}".format(module.params["file_system"], dest)
    scp = SCPClient(ssh.get_transport())
    try:
        scp.put(module.params["local_file"], full_remote_path)
    except:
        time.sleep(10)
        temp_size = verify_remote_file_exists(
            module, dest, file_system=module.params["file_system"]
        )
        if int(temp_size) == int(file_size):
            pass
        else:
            module.fail_json(
                msg="Could not transfer file. There was an error "
                "during transfer. Please make sure remote "
                "permissions are set.",
                temp_size=temp_size,
                file_size=file_size,
            )
    scp.close()
    ssh.close()
    return True