community.windows.win_mapped_drive 模块 – 为用户映射网络驱动器

注意

此模块是 community.windows 集合(版本 2.3.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.windows

要在 playbook 中使用它,请指定:community.windows.win_mapped_drive

概要

  • 允许您修改各个用户的映射网络驱动器。

  • 还支持 UNC 形式的 WebDAV 端点。

参数

参数

注释

letter

字符串 / 必需

要映射的网络路径的字母。

此字母不得已在 Windows 中使用。

password

字符串

用于测试初始连接的 username 的密码。

这不会与映射驱动器一起保存,请使用 community.windows.win_credential 模块来为主机持久化用户名和密码。

path

path

要将驱动器映射到的 UNC 路径。

如果指向 WebDAV 位置,则仍然必须采用 UNC 路径格式 \\hostname\path,而不是 URL,有关更多详细信息,请参阅示例。

要指定 https WebDAV 路径,请在主机名后添加 @SSL。要指定自定义 WebDAV 端口,请在 UNC 路径的 @SSL 或主机名部分后添加 @<端口号>,例如 \\server@SSL@1234\\server@1234

如果 state=present,则这是必需的。

如果 state=absent 且未设置*path*,则无论目标如何,该模块都将删除映射的驱动器。

如果 state=absent 且设置了*path*,则如果路径与映射驱动器的目标不匹配,该模块将抛出错误。

state

字符串

如果 present,将确保映射的驱动器存在。

如果 absent,将确保映射的驱动器不存在。

选项

  • "absent"

  • "present" ←(默认)

username

字符串

用于测试初始连接的用户名。

这不会与映射驱动器一起保存,请使用 community.windows.win_credential 模块来为主机持久化用户名和密码。

如果映射的驱动器需要使用自定义凭据进行身份验证,并且不能使用 become 或 CredSSP,则这是必需的。

如果使用 become 或 CredSSP,则会自动使用使用 community.windows.win_credential 保存的任何凭据。

注意

注意

  • 您不能使用此模块来访问另一个 Ansible 任务中的映射驱动器,使用此模块映射的驱动器仅在通过控制台或 RDP 与用户交互式登录时才可访问。

  • 当远程路径需要身份验证时,建议使用 become 或 CredSSP 运行此模块。

  • 当使用 become 或 CredSSP 时,该任务将有权访问存储在用户保管库中的任何本地凭据。

  • 如果 become 或 CredSSP 不可用,则可以使用 *username* 和 *password* 选项进行初始身份验证,但这些选项不会持久化。

  • WebDAV 路径必须安装 WebDAV 客户端功能,此模块才能映射这些路径。默认情况下,桌面 Windows 版本会安装此功能,但 Windows Server 主机需要使用 ansible.windows.win_feature 安装 WebDAV-Redirector 功能。

另请参阅

另请参阅

community.windows.win_credential

管理凭据管理器中的 Windows 凭据。

示例

- name: Create a mapped drive under Z
  community.windows.win_mapped_drive:
    letter: Z
    path: \\domain\appdata\accounting

- name: Delete any mapped drives under Z
  community.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:
    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:
        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:
        letter: M
        path: \\SERVER\C$
        state: present
  vars:
    # become is required to save and retrieve the credentials in the tasks
    ansible_become: true
    ansible_become_method: runas
    ansible_become_user: '{{ ansible_user }}'
    ansible_become_pass: '{{ ansible_password }}'

- name: Create mapped drive with credentials that do not persist on the next logon
  community.windows.win_mapped_drive:
    letter: M
    path: \\SERVER\C$
    state: present
    username: '{{ ansible_user }}'
    password: '{{ ansible_password }}'

# This should only be required for Windows Server OS'
- name: Ensure WebDAV client feature is installed
  ansible.windows.win_feature:
    name: WebDAV-Redirector
    state: present
  register: webdav_feature

- name: Reboot after installing WebDAV client feature
  ansible.windows.win_reboot:
  when: webdav_feature.reboot_required

- name: Map the HTTPS WebDAV location
  community.windows.win_mapped_drive:
    letter: W
    path: \\live.sysinternals.com@SSL\tools  # https://live.sysinternals.com/tools
    state: present

作者

  • Jordan Borean (@jborean93)