使用 Ansible 在 Windows 客户机上映射网络驱动器 [英] Mapping a Network Drive on a Windows Guest using Ansible

查看:33
本文介绍了使用 Ansible 在 Windows 客户机上映射网络驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ansible 在 Windows 客户机上自动执行一些任务,但在映射网络驱动器时遇到了一些问题.

I'm trying to automate some tasks on Windows guests using Ansible, and I'm running into some issues mapping a network drive.

我想要做的是映射驱动器,对其执行一些操作(在我的示例中,我只是尝试列出文件),然后取消映射.

What I'm trying to do is map the drive, do something to it (in my example here, I just try to list the files), and then unmap it.

当我运行 Ansible 时,输出表明共享驱动器已成功映射,但列出文件和取消映射都会导致错误,指出驱动器不存在.(名为‘K’的驱动器不存在.")

When I run Ansible, the output suggests that the shared drive was mapped successfully, but listing the files and unmapping both result in errors that state that the drive doesn't exist. ("A drive with the name 'K' does not exist.")

当我在运行 Ansible 后登录 Windows 来宾时,驱动器被映射.

When I login to the Windows guest after running Ansible, the drive is mapped.

如果我在登录来宾时运行 Ansible,则驱动器仅在我注销并重新登录后才可见.我用来挂载驱动器的脚本还在来宾上创建了一个用于调试的文件,即使我已登录,该文件也会出现.我不需要注销并再次重新登录即可使其可见.所以似乎只有网络驱动器映射需要注销和登录才能生效.

If I run Ansible while I am logged in the guest, the drive only becomes visible after I logout and log back in again. The script I use to mount the drive also creates a file on the guest for debugging purposes, and the file does appear even when I'm logged in. I don't need to logout and back in again for it to become visible. So it seems only the network drive mapping requires a logout and login to take effect.

我也试过net use"来映射驱动器,结果是一样的.

I also tried "net use" to map the drive, and the results were the same.

我的 Ansible 剧本是这样的.(我省略了一些敏感部分.)

My Ansible playbook looks like this. (I have left out some sensitive parts.)

  tasks:
      - name: Mount share
        script: scripts/mount.ps1 {{ share }}
      - name: Test
        script: scripts/test.ps1
        register: test
      - name: Test stdout
        debug: msg="{{ test.stdout }}"
      - name: Test stderr
        debug: msg="{{ test.stderr }}"
      - name: Umount share
        script: scripts/umount.ps1

mount.ps1.

param([string]$share)
$share | Out-File c:\ansible-test\debug.txt
New-PSDrive -Name "K" -PSProvider FileSystem -Root "$share" -Persist

test.ps1

Get-ChildItem K:\

umount.ps1

Remove-PSDrive "K"

推荐答案

不幸的是,这是 Ansible 与 Windows 通信的方式.我遇到过类似的问题,命令和软件包安装没有按预期工作.

Unfortunately this is down the way Ansible communicates with windows. I've had similar issues where commands and package installation didn't work as expected.

当 Ansible 通过 WinRM 与 Windows 框通信时,它会启动批处理连接,而不是完整会话.映射驱动器是需要完整会话的任务之一,但 Ansible 运行的每个任务都会创建它自己的批处理连接,因此当您在断开连接后立即映射驱动器时,您会丢失该映射,因为也没有实际用户注册它.

When Ansible communicates over WinRM to the windows box it initiates a batch connection, not a full session. Mapping drives is one of those tasks that requires a full session, but each task that Ansible runs creates it's own batch connection so when you map a drive as soon as it disconnects, you loose that mapping because there is no actual user to register it too.

这里唯一的解决方法是创建一个 .ps1 来映射驱动器,然后使用 Invoke-Command.

The only work around you have here is to create a .ps1 to map the drive then use Invoke-Command.

这是我的工作:

- name: map drive and run a task
  script: files/mapdrive.ps1 -network_password "{{ network_password }" -command_to_run "{ Copy-Item Y:\file_to_copy.txt C:\Some\Were }"

我的 ps1 看起来像这样:

and my ps1 looks like this :

param(
  $network_password,
  $command_to_run
)
$PWord = ConvertTo-SecureString $network_password -AsPlainText -Force
$myCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\user",$PWord
New-PSDrive -Name "Y" -PSProvider "FileSystem" -Root "\\remoteserver\share" -Credential $myCreds
Invoke-Command -ScriptBlock $command_to_run

显然,Ansible 的未来版本将采用一种成为"形式,这将允许这些持久会话使此类任务变得更加容易,但这可能需要几年的时间,并且至少会提前 2 或 3 个版本.

Apparently future releases of Ansible will employ a form of "become" which will allow these persistent sessions making this type of task a lot easier, but that could be a couple of years away and at least 2 or 3 releases ahead.

这篇关于使用 Ansible 在 Windows 客户机上映射网络驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆