# Windows Playbook how to not have a remote connection for every task

**URL:** <https://forum.ansible.com/t/windows-playbook-how-to-not-have-a-remote-connection-for-every-task/34996>\
**Category:** Ansible Project\
**Tags:** windows, awx\
**Created:** [April 15, 2021, 9:33pm UTC](https://forum.ansible.com/t/windows-playbook-how-to-not-have-a-remote-connection-for-every-task/34996 "2021-04-15T21:33:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel2](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Daniel2](https://forum.ansible.com/u/Daniel2)\
**Post date:** [April 15, 2021, 9:33pm UTC](https://forum.ansible.com/t/windows-playbook-how-to-not-have-a-remote-connection-for-every-task/34996/1 "2021-04-15T21:33:37Z")

</div>

Working on a playbook, using the following windows modules: win\_mapped\_drive and win\_robocopy.  
I have been using net use commands since the late 80’s with 3Com Lan Manager, so I know what I am doing and I can do what I want at the command line.

win\_mapped\_drive x:  
block of stuff

win\_mapped\_drive z:  
block of stuff

win\_robocopy  
block of stuff

I am running this in AWX, and I see that when it does the first module it makes the connection and does it  
Then it makes a connection and does the second mapped drive  
Then it makes a connection and errors out at the robocopy, saying that x: does not exist.

I tried making it persistent with become, runas, SYSTEM. Then I get a permission issue with win\_robocopy module and I had to go into the registry and kill the mapped drives.

I think it is “logging off” after every task, is there a way to make a single connection and keep the same connection for the entire playbook?

Thank you

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [April 15, 2021, 9:45pm UTC](https://forum.ansible.com/t/windows-playbook-how-to-not-have-a-remote-connection-for-every-task/34996/2 "2021-04-15T21:45:21Z")

</div>

Each task is run in it’s own shell, you cannot map a drive and expect to be able to use it in an Ansible task. It even says it in the documentation for win\_mapped\_drive [https://docs.ansible.com/ansible/latest/collections/community/windows/win\_mapped\_drive\_module.html](https://docs.ansible.com/ansible/latest/collections/community/windows/win_mapped_drive_module.html)

> You cannot use this module to access a mapped drive in another Ansible task, drives mapped with this module are only accessible when logging in interactively with the user through the console or RDP.

What you are meant to do is just use the UNC path and use become on the task that needs to access that UNC path so the credentials are delegated. If you need to use 2 separate credentials you might be able to make it work with win\_credential like

- name: run tasks with become to access the cred store

become: yes # Required so you can access the cred store

become\_method: runas

vars:  
ansible\_become\_user: ‘{{ ansible\_user }}’  
ansible\_become\_pass: ‘{{ ansible\_password }}’  
block:

- win\_credential:  
name: server1  
type: domain\_password  
username: user1  
secret: password  
state: present

- win\_credential:  
name: server2  
type: domain\_password  
username: user2  
secret: password  
state: present

- win\_command: robocopy \server1\share\folder \server2\share\folder

---

<div class="post-metadata">

**Author:** ![Daniel2](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Daniel2](https://forum.ansible.com/u/Daniel2)\
**Post date:** [April 15, 2021, 9:49pm UTC](https://forum.ansible.com/t/windows-playbook-how-to-not-have-a-remote-connection-for-every-task/34996/3 "2021-04-15T21:49:12Z")

</div>

Thank you. I guess I missed that while reading, honestly I mostly skip down to the usage.  
I did not think about using the UNC path and yes, I will be able to utilize the same account for both servers thank goodness.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [April 15, 2021, 9:55pm UTC](https://forum.ansible.com/t/windows-playbook-how-to-not-have-a-remote-connection-for-every-task/34996/4 "2021-04-15T21:55:04Z")

</div>

Awesome if it’s the same account then it’s a simple

- win\_command: my command  
become: yes  
become\_method: runas  
vars:  
ansible\_become\_user: username  
ansible\_become\_pass: password

You may also want to add ‘ansible\_become\_flags: logon\_type=new\_credentials logon\_flags=netcredentials\_only’ to the vars if this is purely for outbound authentication. This will keep all local actions running as your connection user but outbound auth to the file servers will be with the become user.
