facing the problem in mapping network drive

Hi ,

help is appreciated

playbook:mount.yml

  • hosts: localhost
    gather_facts: false
    vars_files:
  • /etc/ansible/group_vars/host_vars
    tasks:
  • name: create a mapped drive under Z
    win_mapped_drive:
    letter: Z
    path: \10.237.163.116\test
    username: test
    password: Password123

host_vars:
windows machine details:

ip: “10.234.224.199”
username: “Administrator”
password: “netapp”
ansible_connection: “winrm”
ansible_winrm_server_cert_validation: “ignore”

i am getting below error when run the playbook:
fatal: [localhost]: UNREACHABLE! => {“changed”: false, “msg”: “ssl: auth method ssl requires a username”, “unreachable”: true}

Hello,

I am setting the username and password:
i have a volume name “test” in my storage box and i want to map it windows machine:

details in host_vars are my machine details:

volume details:

letter: Z
path: \10.237.163.116\test
username: test
password: Password123

i am expecting the playbook use host_vars file to login to windows machine and mount the volume in z drive.

The error you have is nothing to do with mapped drives but that Ansible is unable to authenticate itself over the WinRM connection. YOu need to set ansible_user and ansible_password for Ansible to use as the connection credentials.

Also please note that while you can map a network drive using win_mapped_drive in Ansible, you cannot use that mapped drive in Ansible tasks. That mapping is just for interactive logons. If you want to access files from a network path in Ansible you need to use the UNC path and potentially use become to add outbound creds to your process. We have an example of that here https://docs.ansible.com/ansible/latest/user_guide/become.html#become-flags-for-windows.

Thanks Jordan,

I used become in my playbook.

  • hosts: [win]
    gather_facts: false
    tasks:
  • name: create a mapped drive
    win_mapped_drive:
    state: present
    letter: D
    path: \10.237.163.116\test

vars:

become is required to save and retrieve the credentials in the tasks

ansible_become: yes
ansible_become_method: runas
ansible_become_user: test
ansible_become_pass: password@123
ansible_become_flags: logon_type=new_credentials logon_flags=netcredentials_only

Playbook is running without any errors.

But i am not seeing the network drive in my windows OS.
net use:
Unavailable T: \10.237.163.116\test Microsoft Windows Network

what is that i am missing here. what is the solution to map the network drive.

So the drive mapping is present but Windows is unable to map the drive on logon. You can use the win_credential [1] to store a credential that Windows will use for mapped drives. There’s even an example of this in the win_mapped_drive [2] docs. If you are running the net use command through Ansible and expecting to see that mapped drive available then you are out of luck. Mapped drives are only for use with interactive logons and not for use in Ansible, Ansible can only manage the mappings but not use them.

[1] https://docs.ansible.com/ansible/latest/modules/win_credential_module.html
[2] https://docs.ansible.com/ansible/latest/modules/win_mapped_drive_module.html

Hi Jordan,

I tried the below playbook to store the credential that Windows will use for mapped drives. But it is throwing the below error :

  • hosts: [win]
    gather_facts: false
    tasks:
  • name: Save the network credentials required for the mapped drive
    win_credential:
    name: 10.224.233.199 -----> windows machine IP
    type: domain_password
    state: present
    #persistence: local
    #secret_format: text
    #update_secret: always
    username: Administrator-----> windows machine username
    secret: opteamix------> windows machine password
    become: yes
    become_method: runas

vars:

become is required to save and retrieve the credentials in the tasks

ansible_become_user: apple ----- username of the drive to be mapped.
ansible_become_pass: barharbour -----password of the drive to be mapped.
ansible_become_flags: logon_type=new_credentials logon_flags=netcredentials_only

fatal: [10.234.224.199]: FAILED! => {
“changed”: false,
“msg”: “Unhandled exception while executing module: Exception calling "GetCredential" with "2" argument(s): "Failed to access the user’s credential store, run the module with become"”

Thanks,
Mahesh

Hi,

It may be easier to use PowerShell within an Ansible caller.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7