【发布时间】:2025-12-26 14:10:16
【问题描述】:
我正在尝试使用 Ansible 在 Windows 客户机上自动执行一些任务,但在映射网络驱动器时遇到了一些问题。
我要做的是映射驱动器,对其做一些事情(在我的示例中,我只是尝试列出文件),然后取消映射它。
当我运行 Ansible 时,输出表明共享驱动器已成功映射,但列出文件和取消映射都会导致错误,指出驱动器不存在。 (“名为“K”的驱动器不存在。”)
当我在运行 Ansible 后登录到 Windows 客户机时,驱动器被映射。
如果我在来宾登录时运行 Ansible,则驱动器仅在我注销并重新登录后才可见。我用来挂载驱动器的脚本还会在来宾上创建一个文件以进行调试,即使我登录该文件也会出现。我不需要注销并再次登录以使其可见。所以看来只有网盘映射需要注销登录才能生效。
我也试过“net use”来映射驱动,结果还是一样。
我的 Ansible 剧本看起来像这样。 (我省略了一些敏感部分。)
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"
【问题讨论】:
-
如果我将 ps1 脚本合并为一个,它可以工作。
标签: windows powershell ansible winrm