Repository: ansible/ansible

Issue:
use vmware_guest_file_operation module to copy localhost file to VM without network, the copy doesn't work.
<!---
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. -->

##### SUMMARY
<!---   -->
Use vmware_guest_file_operation module to copy localhost file to VM without network, with error: 
initiateFileTransferToGuest : problem during file transfer"
##### 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-->
vmware_guest_file_operation

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
```

##### 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).-->

DEFAULT_REMOTE_USER(/etc/ansible/ansible.cfg) = root
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False

##### 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.-->
Red Hat Enterprise Linux Workstation release 6.5 (Santiago)------running ansible from
vSphere vCenter: 6.5
##### 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. -->
Want to copy file to vm (judytestvm1) in vsphere vcenter(10.9.242.11). 
There is a local file (/root/judy/judy_vcenter/a.txt) in ansible running host.
<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: Copy file to VM for network configuration
  hosts: localhost
  tasks:
  - name: copy network config file to VM
    vmware_guest_file_operation:
      hostname: 10.9.242.11
      username: administrator@vsphere.local
      password: passw0rd
      datacenter: Datacenter
      validate_certs: no
      vm_id: judytestvm1
      vm_username: root
      vm_password: r00t
      copy:
        src: "/root/judy/judy_vcenter/a.txt"
        dest: "/root/a.txt"
        overwrite: True
    delegate_to: localhost
    register: facts

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
a.txt can be copied to the VM even if there is a file a.txt in VM, the file will be overwritten.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->


<!--- Paste verbatim command output between quotes below -->
```
TASK [copy network config file to VM] ******************************************************************************************************
task path: /root/judy/judy_vcenter/file2vm.yml:5
Using module file /usr/lib/python2.6/site-packages/ansible/modules/cloud/vmware/vmware_guest_file_operation.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333 `" && echo ansible-tmp-1529489357.52-163968408326333="` echo /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333 `" ) && sleep 0'
<localhost> PUT /root/.ansible/tmp/ansible-local-1171Qn0_bR/tmp2AWkEb TO /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333/vmware_guest_file_operation.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333/ /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333/vmware_guest_file_operation.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333/vmware_guest_file_operation.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1529489357.52-163968408326333/ > /dev/null 2>&1 && sleep 0'
fatal: [127.0.0.1 -> localhost]: FAILED! => {
    "changed": false, 
    "invocation": {
        "module_args": {
            "cluster": null, 
            "copy": {
                "dest": "/root/a.txt", 
                "overwrite": true, 
                "src": "/root/judy/judy_vcenter/a.txt"
            }, 
            "datacenter": "Datacenter", 
            "directory": null, 
            "fetch": null, 
            "folder": null, 
            "hostname": "10.9.242.11", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": 443, 
            "username": "administrator@vsphere.local", 
            "validate_certs": false, 
            "vm_id": "judytestvm1", 
            "vm_id_type": "vm_name", 
            "vm_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "vm_username": "root"
        }
    }, 
    "msg": "initiateFileTransferToGuest : problem during file transfer", 
    "uuid": "420f7139-1da5-8618-5506-4f78e55e4a2c"
}
	to retry, use: --limit @/root/judy/judy_vcenter/file2vm.retry

PLAY RECAP *********************************************************************************************************************************
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=1 
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @dav1x @stravassac @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@judyQD run it in verbose mode
@stravassac run it in verbose mode, the error still there.
```
ansible-playbook ./file2vm.yml  -v
Using /etc/ansible/ansible.cfg as config file

PLAY [Copy file to VM for network configuration] *******************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [127.0.0.1]

TASK [copy network config file to VM] ******************************************************************************************************
fatal: [127.0.0.1 -> localhost]: FAILED! => {"changed": false, "msg": "initiateFileTransferToGuest : problem during file transfer", "uuid": "420f7139-1da5-8618-5506-4f78e55e4a2c"}
	to retry, use: --limit @/root/judy/judy_vcenter/file2vm.retry

PLAY RECAP *********************************************************************************************************************************
127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=1   
```
@stravassac Could you pl have a look and help me ? Thank you in advance.
@stravassac @warthog9 @dav1x @Akasurde  I have one more question. The Virtual Machine I tested in vCenter has not been configured network. Did this affect the playbook's result? But I tested in a VM with network, only directory operation worked, both fetch and copy failed. It's very nice of you to help me as I was blocked. Thanks.
@judyQD this work without network.
Your error is in the copy function, the http return code is not equal 200
Could you locate vmware_guest_file_operation.py on your drive and edit it to replace on line 351:
```
self.module.fail_json(msg='initiateFileTransferToGuest : problem during file transfer',
                                      uuid=self.vm.summary.config.uuid)
```
```
self.module.fail_json(msg='initiateFileTransferToGuest : %s' % info,
                                      uuid=self.vm.summary.config.uuid)
```
It will display more error.
I'll make a fix for it, but if you're hurry edit the file below. It'll not fix your problem but explain it

@stravassac thanks for your reply. The error info shows as below:
 "msg": "initiateFileTransferToGuest : {'url': 'https://10.9.242.10:443/guestFile?id=19&token=52020ae5-0b17-74a5-16d7-81b2af0d95f819', 'status': -1, 'msg': 'Request failed: <urlopen error timed out>'}"
Could you pl help check what's wrong? Thanks
@stravassac  BTW, I found the IP addr 10.9.242.10 is the ip of datastore where the vm is located. Is there any wrong with the datastore of port 443? Looking forward to your reply and help.

Resolving patch:
diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py b/lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py
--- a/lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_guest_file_operation.py
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-# Copyright: (c) 2017, Stéphane Travassac <stravassac () gmail.com>
+# Copyright: (c) 2017, Stéphane Travassac <stravassac@gmail.com>
 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
 
 from __future__ import absolute_import, division, print_function
@@ -348,7 +348,7 @@ def copy(self):
 
             status_code = info["status"]
             if status_code != 200:
-                self.module.fail_json(msg='initiateFileTransferToGuest : problem during file transfer',
+                self.module.fail_json(msg='problem during file transfer, http message:%s' % info,
                                       uuid=self.vm.summary.config.uuid)
         except vim.fault.FileAlreadyExists:
             result['changed'] = False


Test patch: