Repository: ansible/ansible

Issue:
nxos_file_copy broken in devel
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
nxos_file_copy

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
/shared # ansible --version
  2753 1504032895.05281: starting run
ansible 2.4.0
  config file = /shared/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Dec 22 2016, 09:22:15) [GCC 6.2.1 20160822]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
```
/shared # cat ansible.cfg
[defaults]
host_key_checking = False
inventory = hosts
ansible_python_interpreter = python
retry_files_enabled = False
```

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Running from Alpine Linux 3.5 (docker container)
Managed device: NXOS: version 7.0(3)I2(2d)
##### SUMMARY
<!--- Explain the problem briefly -->
nxos_file_copy works fine in Ansible 2.3.2.0, but in the latest develop branch, an error occurs. I have tried this back to back on the same docker container by installing/uninstalling ansible 2.4 and 2.3.2.0. File copy works fine in 2.3.2.0.

```
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
}
```
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
Install ansible from devel branch.
Run against NXOS.
<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: NXOS file copy test
  hosts: nxos
  connection: local
  vars:
    provider:
      username: "{{ ansible_ssh_user }}"
      password: "{{ ansible_ssh_pass }}"
      transport: cli
      host: "{{ inventory_hostname }}"
  tasks:

    - name: Copy file to device
      nxos_file_copy:
        local_file: /shared/testfile
        remote_file: "testfile"
        provider: "{{ provider }}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
File should be copied
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
file is not copied.
<!--- Paste verbatim command output between quotes below -->

Full output of failed run, uninstall ansible 2.4, install ansible 2.3.2.0, copy is successful.
https://gist.github.com/bobbywatson3/a75da7c94e5a890377386311b94958a1




Discussion:
cc @GGabriele @jedelman8 @mikewiebe @privateip @rahushen @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_file_copy.py b/lib/ansible/modules/network/nxos/nxos_file_copy.py
--- a/lib/ansible/modules/network/nxos/nxos_file_copy.py
+++ b/lib/ansible/modules/network/nxos/nxos_file_copy.py
@@ -61,9 +61,8 @@
 EXAMPLES = '''
 - nxos_file_copy:
     local_file: "./test_file.txt"
-    username: "{{ un }}"
-    password: "{{ pwd }}"
-    host: "{{ inventory_hostname }}"
+    remote_file: "test_file.txt"
+    provider: "{{ cli }}"
 '''
 
 RETURN = '''
@@ -144,9 +143,11 @@ def transfer_file(module, dest):
     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']
+    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())
diff --git a/lib/ansible/plugins/action/nxos.py b/lib/ansible/plugins/action/nxos.py
--- a/lib/ansible/plugins/action/nxos.py
+++ b/lib/ansible/plugins/action/nxos.py
@@ -59,13 +59,6 @@ def run(self, tmp=None, task_vars=None):
             pc.password = provider['password'] or self._play_context.password
             pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
             pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)
-            self._task.args['provider'] = provider.update(
-                host=pc.remote_addr,
-                port=pc.port,
-                username=pc.remote_user,
-                password=pc.password,
-                ssh_keyfile=pc.private_key_file
-            )
             display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr)
             connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
 


Test patch: