Repository: ansible/ansible

Issue:
fetch plugin sends file to the remote node when using scp_if_ssh=True
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
plugins/action/fetch.py

##### ANSIBLE VERSION
```
ansible 2.2.0.0
  config file = /home/ubuntu/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
```
[ssh_connection]
scp_if_ssh = True
```

##### OS / ENVIRONMENT
"N/A" 

##### SUMMARY
When using the `fetch` module with `scp` rather than `sftp`, ansible tries to send the file from the ansible machine to the remote machine

##### STEPS TO REPRODUCE
1.- Create `/home/ubuntu/test` file in the remote machine
2.- Create `ansible.cfg` in the current directory with `scp_if_ssh=True`
3.- Run the following playbook

fetch.yml
```
---
- hosts: all
  tasks:
    - name: Fetch without become
      fetch: src=/home/ubuntu/test dest=test flat=yes fail_on_missing=yes
```

##### EXPECTED RESULTS
test file should be copied FROM the remote machine to the ansible host

##### ACTUAL RESULTS
Ansible tries to copy the file FROM the ansible host TO the remote machine

```
PLAYBOOK: fetch.yml ************************************************************
1 plays in fetch.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/core/system/setup.py
<remote.example.com> ESTABLISH SSH CONNECTION FOR USER: None
<remote.example.com> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r remote.example.com '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933 `" && echo ansible-tmp-1479938029.51-148266722234933="` echo $HOME/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933 `" ) && sleep 0'"'"''
<remote.example.com> PUT /tmp/tmpIPCKL8 TO /home/ubuntu/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933/setup.py
<remote.example.com> SSH: EXEC scp -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r /tmp/tmpIPCKL8 '[remote.example.com]:/home/ubuntu/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933/setup.py'
<remote.example.com> ESTABLISH SSH CONNECTION FOR USER: None
<remote.example.com> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r remote.example.com '/bin/sh -c '"'"'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933/ /home/ubuntu/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933/setup.py && sleep 0'"'"''
<remote.example.com> ESTABLISH SSH CONNECTION FOR USER: None
<remote.example.com> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt remote.example.com '/bin/sh -c '"'"'/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933/setup.py; rm -rf "/home/ubuntu/.ansible/tmp/ansible-tmp-1479938029.51-148266722234933/" > /dev/null 2>&1 && sleep 0'"'"''
ok: [remote.example.com]

TASK [Fetch without become] ****************************************************
task path: /home/ubuntu/fetch.yml:4
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/core/files/stat.py
<remote.example.com> ESTABLISH SSH CONNECTION FOR USER: None
<remote.example.com> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r remote.example.com '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372 `" && echo ansible-tmp-1479938030.39-24433390446372="` echo $HOME/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372 `" ) && sleep 0'"'"''
<remote.example.com> PUT /tmp/tmpG5jo2n TO /home/ubuntu/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372/stat.py
<remote.example.com> SSH: EXEC scp -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r /tmp/tmpG5jo2n '[remote.example.com]:/home/ubuntu/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372/stat.py'
<remote.example.com> ESTABLISH SSH CONNECTION FOR USER: None
<remote.example.com> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r remote.example.com '/bin/sh -c '"'"'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372/ /home/ubuntu/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372/stat.py && sleep 0'"'"''
<remote.example.com> ESTABLISH SSH CONNECTION FOR USER: None
<remote.example.com> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r -tt remote.example.com '/bin/sh -c '"'"'/usr/bin/python /home/ubuntu/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372/stat.py; rm -rf "/home/ubuntu/.ansible/tmp/ansible-tmp-1479938030.39-24433390446372/" > /dev/null 2>&1 && sleep 0'"'"''
<remote.example.com> FETCH /home/ubuntu/test TO /home/ubuntu/test
<remote.example.com> SSH: EXEC scp -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/ubuntu/.ansible/cp/ansible-ssh-%h-%p-%r /home/ubuntu/test '[remote.example.com]:/home/ubuntu/test'
fatal: [remote.example.com]: FAILED! => {
    "failed": true,
    "msg": "failed to transfer file to /home/ubuntu/test:\n\n/home/ubuntu/test: No such file or directory\n"
}
        to retry, use: --limit @/home/ubuntu/fetch.retry

PLAY RECAP *********************************************************************
remote.example.com            : ok=1    changed=0    unreachable=0    failed=1
```



Discussion:
git bisect shows that 8e47b9bc70e33534c308298df4b6c8c79886fef1 is the first commit which introduced the bug
@Ssawa Would you be able to take a look at this, this looks like it might have been introduced by your change `Handle 'smart' scp_if_ssh option for fetch ` in https://github.com/ansible/ansible/commit/8e47b9bc70e33534c308298df4b6c8c79886fef1

Thanks

Resolving patch:
diff --git a/lib/ansible/plugins/connection/ssh.py b/lib/ansible/plugins/connection/ssh.py
--- a/lib/ansible/plugins/connection/ssh.py
+++ b/lib/ansible/plugins/connection/ssh.py
@@ -628,7 +628,10 @@ def _file_transport_command(self, in_path, out_path, sftp_action):
                 cmd = self._build_command('sftp', to_bytes(host))
                 in_data = u"{0} {1} {2}\n".format(sftp_action, shlex_quote(in_path), shlex_quote(out_path))
             elif method == 'scp':
-                cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, shlex_quote(out_path)))
+                if sftp_action == 'get':
+                    cmd = self._build_command('scp', u'{0}:{1}'.format(host, shlex_quote(out_path)), in_path)
+                else:
+                    cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, shlex_quote(out_path)))
                 in_data = None
 
             in_data = to_bytes(in_data, nonstring='passthru')


Test patch: