ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
193 stars 152 forks source link

win_mapped_drive not showing drives in a chain of tasks #539

Closed Pshemas closed 8 months ago

Pshemas commented 8 months ago

I'm trying to use win_mapped_drive in a chain of tasks (playbook). The idea is to map drive temporarily on Windows devices and then run powershell script that would use files in share. For example:

    - name: add networkshare
      community.windows.win_mapped_drive:
        letter: x
        path: \\192.168.1.122\installers
        state: present
        username: someuser
        password: somepassword

    - name: install inkscape via lan
      ansible.windows.win_powershell:
        script: |
          cd X:\inkscape ;
          msiexec /i inkscape_x64.msi /qn ALLUSERS=1 

But sadly it seems that the second task does not see the mapped drive. I had similar issue when I've tried doing net use and msiexec in separate steps (tasks) like here. If I did both in a single task it worked.

I guess it's very likely I'm doing something wrong, but after flipping through docs and trying different options I'm out of ideas.

jborean93 commented 8 months ago

Have a look at the notes of the modules documentation

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.

Mapped drives are just for interactive uses and won't ever work inside another Ansible task. If all you need is to authenticate with a network endpoint with custom credentials you can use the UNC path and become

- name: install inkscape
  ansible.windows.win_package:
    path: \\192.168.1.122\installers\inkscape\inkscape_x64.msi
    arguments: ALLUSERS=1
    state: present
  become: true
  become_method: runas
  become_flags: logon_type=new_credentials logon_flags=netcredentials_only
  vars:
    ansible_become_user: someuser
    ansible_become_pass: somepassword

It will access the UNC path directly but with the new_credentials become flags it will now use the become credentials provided for authentication.

Pshemas commented 8 months ago

thank you @jborean93 . I overlooked this part of the docs.

Any suggestions on solving the same problem while using chocolatey packages stored on SMB share?

In more general sense - is there a way to mount a network share so it would be usable by other tasks some other way? I see several workflows where it would make things much easier (mount first, then use it).

jborean93 commented 8 months ago

Use the become stuff I shared with a UNC path for credential support, as for mapped drives just don't use them. Always use the UNC path.