Help using Win_Copy Module From Windows Host To Windows Host

124 views
Skip to first unread message

Paul Bull

unread,
Sep 13, 2020, 5:50:17 AM9/13/20
to Ansible Project

Hi Guys,

I would really welcome some help and input regarding the above in my lab.

I have a server with the file and the path is correct, as well as the username and pass.

This is the playbook:

# copy files
- hosts: windows

  tasks:
   - name: copy files from share
     win_copy:
        src: \\2019-ANSIBLE\c$\share\certnew.cer
        dest: \\win19-ansible-2\c$\users\Administrator\Desktop\test\certnew.cer
        remote_src: yes
     vars:
      ansible_become_user: Administrator
      ansible_become_pass: ********


And Error:


Using module file /usr/lib/python3/dist-packages/ansible/modules/windows/win_copy.ps1
Pipelining is enabled.
<win19-ansible-2.ansible-ad.net> ESTABLISH WINRM CONNECTION FOR USER: ans...@ANSIBLE-AD.NET on PORT 5986 TO win19-ansible-2.ansible-ad.net
EXEC (via pipeline wrapper)
The full traceback is:
Access is denied
At line:305 char:15
+     if (-not (Test-Path -LiteralPath $src)) {
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (\\2019-ANSIBLE\c$\share\certnew.cer:String) [Test-Path], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand

ScriptStackTrace:
at <ScriptBlock>, <No file>: line 305

System.UnauthorizedAccessException: Access is denied ---> System.ComponentModel.Win32Exception: Access is denied
   --- End of inner exception stack trace ---
   at Microsoft.PowerShell.Commands.FileSystemProvider.ItemExists(String path, ErrorRecord& error)
fatal: [win19-ansible-2.ansible-ad.net]: FAILED! => {
    "changed": false,
    "dest": "\\\\win19-ansible-2\\c$\\users\\Administrator\\Desktop\\test\\certnew.cer",
    "msg": "Unhandled exception while executing module: Access is denied",
    "src": "\\\\2019-ANSIBLE\\c$\\share\\certnew.cer"
}

Looking at the ansible documents examples :


I also tried :

# copy files
- hosts: windows

  tasks:
   - name: copy files from share
     win_copy:
        src: C:\share\certnew.cer
        dest: C:\users\Administrator\Desktop\test\certnew.cer
        remote_src: yes
     vars:
      ansible_become_user: Administrator
      ansible_become_pass: ********

and get:

fatal: [win19-ansible-2.ansible-ad.net]: FAILED! => {"changed": false, "dest": "C:\\users\\Administrator\\Desktop\\test\\certnew.cer", "msg": "Cannot copy src file: 'C:\\share\\certnew.cer' as it does not exist", "src": "C:\\share\\certnew.cer"}

I'm stumped as the paths are correct and exist. Could anyone please help on how you copy from one windows host to another?. As well as from a share on one to another local path.

Much appreciated. Thank you

I tried that way above

jbor...@gmail.com

unread,
Sep 14, 2020, 4:29:06 PM9/14/20
to Ansible Project
You've set the become vars but you haven't actually turned on become, have a read through https://docs.ansible.com/ansible/latest/user_guide/become.html#become-flags-for-windows especially the "copy a file from a fileshare with custom credentials" example.

If you are trying to copy a file from a remove UNC share to the local path of the Windows host, don't use the UNC path for dest, just do 'dest: C:\users\Administrator\Desktop\test\certnew.cer'. You still need the UNC path for src though as the src is how that process finds the file.

Paul Bull

unread,
Sep 14, 2020, 4:44:49 PM9/14/20
to Ansible Project
I will take a look at this when i get home from work in the next few days and work this out.

I presume i do have the right module and win_copy does allow Windows to windows copy then?. Only reason i ask is because of this in the documentation " The win_copy module copies a file on the local box to remote windows locations." When i read that back it suggests to me the local box would be the Ansible host/management box?.

Or would in your opinion a different module be better for windows to windows?. For example the win_shell with a copy-item command?.

Which in my case my ansible host is a Linux mint machine.

Again thank you for your response and taking the time out to reply and help!

jbor...@gmail.com

unread,
Sep 15, 2020, 12:07:46 AM9/15/20
to Ansible Project
No it will work, by default it's meant to copy files from the Ansible controller to the Windows host but with 'remote_src: yes' it changes the context of src to be from the remote Windows host.

Paul Bull

unread,
Sep 15, 2020, 5:09:56 PM9/15/20
to Ansible Project
I copied the example you gave me and it worked. Thanks for pointing me in the right direction and your help!
Reply all
Reply to author
Forward
0 new messages