# Ansible: user is not authenticating to network share

**URL:** <https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602>\
**Category:** Get Help\
**Tags:** winrm, powershell, windows, playbook\
**Created:** [October 10, 2023, 4:42pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602 "2023-10-10T16:42:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mister\_ansible](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mister\_ansible](https://forum.ansible.com/u/mister_ansible)\
**Post date:** [October 10, 2023, 4:42pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/1 "2023-10-10T16:42:10Z")

</div>

I’m having difficulty transferring files from a network share to a disk on the local server, via a remote connection. I am logging into the server: SERVER02, however, permission denied appears, using Wireshark collections on SERVER01, I found that user credentials and passwords are not arriving. It’s strange because doing the local robocopy command in PowerShell works correctly. Also, I’ve used Ansible modules and they didn’t work, this module ansible.windows.win\_powershell was the one that gave me the most information. Below is the code that is executed and its output:

```auto
- name: Copy files from network share folder to Windows server
  hosts: "{{ host_group }}"
  gather_facts: no
  vars:
    ansible_connection: winrm
    ansible_user: "{{ vm_user }}"
    ansible_password: "{{ vm_pass }}"
    ansible_port: 5985
    ansible_winrm_transport: ntlm
    ansible_winrm_server_cert_validation: ignore

  tasks:
    - name: Copia arquivos
      ansible.windows.win_powershell: 
        script: |
          $Username = "{{ vm_user }}"
          $SecurePassword = ConvertTo-SecureString -String "{{ vm_pass }}" -AsPlainText -Force
          $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword
          Enter-PSSession -ComputerName "{{ host_group }}" -Credential $Credential
          robocopy \\SERVER01\I$\APP\MONITOR\ \\SERVER02\I$\JOBS\ /COPYALL /MT /E /b /W:5 /R:5

```

Output execution:

```auto
-------------------------------------------------------------------------------
   ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : terca-feira, 10 de outubro de 2023 10:14:27
   Source = \\SERVER01\I$\APP\MONITOR\
     Dest : \\SERVER02\I$\JOBS\

    Files : *.*

  Options : *.* /S /E /COPYALL /B /MT:8 /R:5 /W:5

------------------------------------------------------------------------------

NOTE : Security may not be copied - Source might not support persistent ACLs.

2023/10/10 10:14:27 ERROR 5 (0x00000005) Accessing Source Directory \\SERVER01\I$\APP\MONITOR\
Access is denied.

```

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [October 10, 2023, 6:48pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/2 "2023-10-10T18:48:00Z")

</div>

Hi,

I might be wrong but I’m pretty sure you can’t `Enter-PSSession` in your script block, as it’s effectively a double-hop.

Perhaps try `Invoke-Command` instead, or have a look to [pssession\_configuration module](https://docs.ansible.com/ansible/latest/collections/community/windows/win_pssession_configuration_module.html).

---

<div class="post-metadata">

**Author:** ![mister\_ansible](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mister\_ansible](https://forum.ansible.com/u/mister_ansible)\
**Post date:** [October 10, 2023, 7:13pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/3 "2023-10-10T19:13:19Z")

</div>

Replacing `Enter-PSSession` with `Invoke-Command` didn’t solve the problem

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [October 10, 2023, 8:05pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/4 "2023-10-10T20:05:44Z")

</div>

Yeah well, that was just a suggestion. It looks like `Invoke-Command` uses PSSession so that won’t cut it indeed.

You’ll have to find a way to create a PSSession elsewhere, and pass your commands through it, or proxy this session, though I’m not sure winrm have those capabilities.  
I’m pretty sure you can get somewhere with pssesion\_configuration module linked in my previous post, though you may have to hack your way through it.

If not, you could always circumvent your issue (if I’m right about double-hop) using ssh as transport protocol instead, which lets you ProxyJump.

Edit: Looking at your script block again, someone correct me if I’m wrong but you can’t `Enter-PSSession` then type command you want to run in the newly created session; that’s not how it works, and that’s why `Invoke-Command` is a thing. That being said, double-hop issue is still a problem you want tot address.  
Just to contextualize a bit, did you try to run your commands directly (not through Ansible modules) from a ps1 script on intended target (so from `"{{ host_group }}"`, according to your playbook) ?

Edit2: Linking it here, just in case: [Making the second hop in PowerShell Remoting - PowerShell | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.3)

---

<div class="post-metadata">

**Author:** ![bmbufalo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bmbufalo/32/295_2.png) [@bmbufalo](https://forum.ansible.com/u/bmbufalo)\
**Post date:** [October 11, 2023, 1:12pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/5 "2023-10-11T13:12:09Z")

</div>

@ptn is correct, this would be a double-hop and those commands will not work. You’ll need to have the Ansible server connect directly to the server(s) in question. I have read that if you use CredSSP to authenticate you can do a double-hop but I haven’t used this.

Something like this may work or at least get you closer:

```auto
tasks:
    - name: Copia arquivos
      ansible.windows.win_copy:
        src: file/path/APP/MONITOR/
        dest: 'I:\JOBS\'

```

Note the `src` here is a file path local to the Ansible server. If you can map the SMB drive on the Ansible server you could also get this result, just replace `file/path/` with the mount path.

Lastly, I know you mentioned you used the modules but did you may look at [community.windows.win\_robocopy](https://docs.ansible.com/ansible/latest/collections/community/windows/win_robocopy_module.html) as a possible solution as well. This one appears to use a `src` and `dest` both on the remote machine unlike the `win_copy` module.

---

<div class="post-metadata">

**Author:** ![mister\_ansible](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mister\_ansible](https://forum.ansible.com/u/mister_ansible)\
**Post date:** [October 11, 2023, 5:01pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/6 "2023-10-11T17:01:04Z")

</div>

@bmbufalo,  
I tested both of your suggestions, as follows:

```auto
	- name: robocopy
      community.windows.win_robocopy:
        src: \\SERVER01\I$\APP\MONITOR\
        dest: \\SERVER02\I$\JOBS\
        flags: /COPYALL /MT /E /b /W:5 /R:5

```

and

```auto
    - name: copy
      ansible.windows.win_copy:
        src: '\\SERVER01\I$\APP\MONITOR\'
        dest: '\\SERVER02\I$\JOBS\'
        remote_src: true

```

however, in both cases it causes a lack of access error

---

<div class="post-metadata">

**Author:** ![mister\_ansible](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mister\_ansible](https://forum.ansible.com/u/mister_ansible)\
**Post date:** [October 11, 2023, 5:11pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/7 "2023-10-11T17:11:44Z")

</div>

@ptn  
yes, configuring a script.ps1 and running it from ansible, as follows:

```auto
     - name: Copia arquivos
       ansible.windows.win_powershell: 
         script: |
           I$\APP\MONITOR\script.ps1

```

also, executing a script.exe written in python, there was also a lack of access 😕

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [October 11, 2023, 6:09pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/8 "2023-10-11T18:09:22Z")

</div>

You still have to provide credentials. Are they hardcoded in your script ? Can you output the script content, as well as the “lack of access” output you’re getting from Ansible ?

Also, did you try running your script manually from Powershell on your remote server ? What gives ?

Also sorry about that, but this is magical haha:

> `script.ps1`

> executing a script.exe

> written in python

Nothing goes together ! 😄

---

<div class="post-metadata">

**Author:** ![mister\_ansible](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mister\_ansible](https://forum.ansible.com/u/mister_ansible)\
**Post date:** [October 17, 2023, 12:53pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/9 "2023-10-17T12:53:20Z")

</div>

sorted out:

```auto
    - name: Copy
      ansible.windows.win_powershell: 
        script: |
          net use Z: \\SERVER01\I$ /user:domain\{{ vm_user }} {{ vm_pass }}
          net use Y: \\SERVER02\I$ /user:domain\{{ vm_user }} {{ vm_pass }}          

          robocopy Z:\APP\MONITOR\ Y:JOBS\ /COPYALL /MT /E /b /W:5 /R:5

          net use Z: /delete
          net use Y: /delete
          
      register: log

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 16, 2023, 12:53pm UTC](https://forum.ansible.com/t/ansible-user-is-not-authenticating-to-network-share/1602/10 "2023-11-16T12:53:29Z")

</div>

Closed on November 16, 2023, 12:53pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
