Repository: ansible/ansible

Issue:
synchronize module is broken in ansible 2.5.3
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
2.5.3
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, 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.-->

##### SUMMARY
<!--- Explain the problem briefly -->
The synchronize module is broken because it tries to use "None" as remote user.
It seems that this problem has been introduced by https://github.com/ansible/ansible/pull/36755 . If I remove the else part, it works again. 
It worked with ansible 2.5.2 and previous versions.

##### 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. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---

- hosts: all
  tasks:
  - name: Synchronize
    synchronize:
      src: /tmp/source
      dest: /tmp/dest

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Synchronize the /tmp/source to remote /tmp/dest

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
It fails when it tries to copy the synchronize.py module (see the expand_path where it tries to expand ~None).
<!--- Paste verbatim command output between quotes below -->
```
<web.dev.int> ESTABLISH LOCAL CONNECTION FOR USER: root
<web.dev.int> EXEC /bin/sh -c 'echo ~None && sleep 0'
<web.dev.int> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~None/.ansible/tmp/ansible-tmp-1526912239.73-206650949785069 `" && echo ansible-tmp-1526912239.73-206650949785069="` echo ~None/.ansible/tmp/ansible-tmp-1526912239.73-206650949785069 `" ) && sleep 0'
<web.dev.int> PUT /root/.ansible/tmp/ansible-local-19667uU7Yb6/tmp4waltg TO ~None/.ansible/tmp/ansible-tmp-1526912239.73-206650949785069/synchronize.py
fatal: [front03.web.dev.int]: FAILED! => {
    "msg": "failed to transfer file to ~None/.ansible/tmp/ansible-tmp-1526912239.73-206650949785069/synchronize.py: [Errno 2] No such file or directory: '~None/.ansible/tmp/ansible-tmp-1526912239.73-206650949785069/synchronize.py'"
}

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/files/synchronize.py b/lib/ansible/modules/files/synchronize.py
--- a/lib/ansible/modules/files/synchronize.py
+++ b/lib/ansible/modules/files/synchronize.py
@@ -372,7 +372,7 @@ def main():
             group=dict(type='bool'),
             set_remote_user=dict(type='bool', default=True),
             rsync_timeout=dict(type='int', default=0),
-            rsync_opts=dict(type='list'),
+            rsync_opts=dict(type='list', default=[]),
             ssh_args=dict(type='str'),
             partial=dict(type='bool', default=False),
             verify_host=dict(type='bool', default=False),
diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py
--- a/lib/ansible/plugins/action/__init__.py
+++ b/lib/ansible/plugins/action/__init__.py
@@ -583,7 +583,7 @@ def _remote_expand_user(self, path, sudoable=True, pathsep=None):
                 expand_path = '~%s' % self._play_context.become_user
             else:
                 # use remote user instead, if none set default to current user
-                expand_path = '~%s' % self._play_context.remote_user or self._connection.default_user or ''
+                expand_path = '~%s' % (self._play_context.remote_user or self._connection.default_user or '')
 
         # use shell to construct appropriate command and execute
         cmd = self._connection._shell.expand_user(expand_path)


Test patch: