command - 使用“win_command:mklink softlinkfile destfile”时找不到mklink.exe\”
问题描述
我正在使用ansible管理云上的几个windows主机,我需要创建一个日志文件并将其链接到另一个文件,所以我使用follow playbook
- name: init the directory structure of windows
hosts: '{{windows_hosts}}'
tasks:
- name: create log file and link it to log directory
win_command: mklink log D:\prod\log
args:
chdir: D:\prod\project
运行此剧本时,可以成功找到主机,但我收到以下错误报告
> TASK [Gathering Facts]
> ********* ok: [111.111.2.40]
>
> TASK [create log file and link it to log directory]
> ********* fatal: [111.231.76.40]: FAILED! => {"changed": false, "cmd": "mklink log
> D:\\prod\\log", "msg": "Exception calling \"SearchPath\" with \"1\"
> argument(s): \"Could not locate the following executable
> mklink.exe\"", "rc": 2}
我在同一目录下的远程主机上尝试了这个命令,它可以成功执行。我不知道该怎么办......
解决方案
win_command 用于直接运行可执行文件。因此没有应用用户的环境,并且您没有在 dos 框或 powershell 窗口中运行
所以“mklink”实际上不是可执行文件——它是 cmd.exe 程序的内置功能。因此,要通过 win_command 运行 mklink,您必须运行 cmd.exe 程序并传递一个参数来告诉它执行“mklink”的作用,如下所示:
win_command: cmd.exe /k mklink log D:\prod\log
推荐阅读
- reactjs - ES5 中的 Material-ui 组件
- powershell - 如何导入/加载 .dll 文件以在 PowerShell 脚本中使用而不会出现“TypeNotFound”错误?
- python - aws s3 响应的 pydantic 数据类型
- c++ - 在有歧义的情况下使用qualified-id访问类成员
- java-8 - ConcurrentModificationException 使用 Collectors.toSet()
- python - ParseFromString 返回二进制文件的文件大小,而不是数据
- javascript - 图像 onClick 更改模态中的内容
- python - 以字符串变量为条件创建折线图
- c# - 来自 BackgroundService 的通知
- mongodb - 使用 MongoDB 中的 ObjectID / Timestamp 字段进行光标分页