Skip to content

Commit

Permalink
migrate-win_mapped_drive:
Browse files Browse the repository at this point in the history
Updating tests with facts insted of literal path.
Update Documentation.
  • Loading branch information
Yaish25491 authored and jborean93 committed Jan 8, 2025
1 parent 3cdcf17 commit c55cc66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
27 changes: 16 additions & 11 deletions plugins/modules/win_mapped_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
description:
- Allows you to modify mapped network drives for individual users.
- Also support WebDAV endpoints in the UNC form.
- While this module can manage mapped drives, you will not be able to use those
mapped drives in any subsequent Ansible task. Mapped drives are only visible
on interactive logons like through RDP and not network logons like WinRM or
SSH that Ansible uses to invoke tasks.
version_added: 2.7.0
options:
letter:
description:
Expand All @@ -22,7 +27,7 @@
description:
- The password for C(username) that is used when testing the initial
connection.
- This is never saved with a mapped drive, use the M(community.windows.win_credential) module
- This is never saved with a mapped drive, use the M(ansible.windows.win_credential) module
to persist a username and password for a host.
type: str
path:
Expand Down Expand Up @@ -50,12 +55,12 @@
username:
description:
- The username that is used when testing the initial connection.
- This is never saved with a mapped drive, the M(community.windows.win_credential) module
- This is never saved with a mapped drive, the M(ansible.windows.win_credential) module
to persist a username and password for a host.
- This is required if the mapped drive requires authentication with
custom credentials and become, or CredSSP cannot be used.
- If become or CredSSP is used, any credentials saved with
M(community.windows.win_credential) will automatically be used instead.
M(ansible.windows.win_credential) will automatically be used instead.
type: str
notes:
- You cannot use this module to access a mapped drive in another Ansible task,
Expand All @@ -73,40 +78,40 @@
Windows Server hosts need to install the C(WebDAV-Redirector) feature using
M(ansible.windows.win_feature).
seealso:
- module: community.windows.win_credential
- module: ansible.windows.win_credential
author:
- Jordan Borean (@jborean93)
'''

EXAMPLES = r'''
- name: Create a mapped drive under Z
community.windows.win_mapped_drive:
ansible.windows.win_mapped_drive:
letter: Z
path: \\domain\appdata\accounting
- name: Delete any mapped drives under Z
community.windows.win_mapped_drive:
ansible.windows.win_mapped_drive:
letter: Z
state: absent
- name: Only delete the mapped drive Z if the paths match (error is thrown otherwise)
community.windows.win_mapped_drive:
ansible.windows.win_mapped_drive:
letter: Z
path: \\domain\appdata\accounting
state: absent
- name: Create mapped drive with credentials and save the username and password
block:
- name: Save the network credentials required for the mapped drive
community.windows.win_credential:
ansible.windows.win_credential:
name: server
type: domain_password
username: username@DOMAIN
secret: Password01
state: present
- name: Create a mapped drive that requires authentication
community.windows.win_mapped_drive:
ansible.windows.win_mapped_drive:
letter: M
path: \\SERVER\C$
state: present
Expand All @@ -118,7 +123,7 @@
ansible_become_pass: '{{ ansible_password }}'
- name: Create mapped drive with credentials that do not persist on the next logon
community.windows.win_mapped_drive:
ansible.windows.win_mapped_drive:
letter: M
path: \\SERVER\C$
state: present
Expand All @@ -137,7 +142,7 @@
when: webdav_feature.reboot_required
- name: Map the HTTPS WebDAV location
community.windows.win_mapped_drive:
ansible.windows.win_mapped_drive:
letter: W
path: \\live.sysinternals.com@SSL\tools # https://live.sysinternals.com/tools
state: present
Expand Down
16 changes: 12 additions & 4 deletions tests/integration/targets/win_mapped_drive/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@
ansible.windows.win_command: 'net use {{test_win_mapped_drive_letter}}:'
register: create_drive_actual

- name: Set expected remote path
set_fact:
expected_remote: "Remote name \\\\{{ ansible_hostname }}\\{{ test_win_mapped_drive_path }}"

- name: assert create mapped drive
assert:
that:
- create_drive is changed
- create_drive_actual.rc == 0
- create_drive_actual.stdout_lines[1] == "Remote name \\\\{{ansible_hostname}}\\{{test_win_mapped_drive_path}}"
- create_drive_actual.stdout_lines[1] == expected_remote

- name: create mapped drive again
win_mapped_drive:
Expand Down Expand Up @@ -87,7 +91,7 @@
that:
- change_drive_target_check is changed
- change_drive_target_actual_check.rc == 0
- change_drive_target_actual_check.stdout_lines[1] == "Remote name \\\\{{ansible_hostname}}\\{{test_win_mapped_drive_path}}"
- change_drive_target_actual_check.stdout_lines[1] == expected_remote

- name: change mapped drive target
win_mapped_drive:
Expand All @@ -100,12 +104,16 @@
ansible.windows.win_command: 'net use {{test_win_mapped_drive_letter}}:'
register: change_drive_target_actual

- name: Set expected remote path
set_fact:
expected_remote2: "Remote name \\\\{{ ansible_hostname }}\\{{ test_win_mapped_drive_path2 }}"

- name: assert change mapped drive target
assert:
that:
- change_drive_target is changed
- change_drive_target_actual.rc == 0
- change_drive_target_actual.stdout_lines[1] == "Remote name \\\\{{ansible_hostname}}\\{{test_win_mapped_drive_path2}}"
- change_drive_target_actual.stdout_lines[1] == expected_remote2

- name: fail to delete mapped drive if target doesn't match
win_mapped_drive:
Expand All @@ -132,7 +140,7 @@
that:
- delete_drive_check is changed
- delete_drive_actual_check.rc == 0
- delete_drive_actual_check.stdout_lines[1] == "Remote name \\\\{{ansible_hostname}}\\{{test_win_mapped_drive_path2}}"
- delete_drive_actual_check.stdout_lines[1] == expected_remote2

- name: delete mapped drive
win_mapped_drive:
Expand Down

0 comments on commit c55cc66

Please sign in to comment.