首页 > 解决方案 > 使用“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}

我在同一目录下的远程主机上尝试了这个命令,它可以成功执行。我不知道该怎么办......

标签: commandansiblemklink

解决方案


win_command 用于直接运行可执行文件。因此没有应用用户的环境,并且您没有在 dos 框或 powershell 窗口中运行

所以“mklink”实际上不是可执行文件——它是 cmd.exe 程序的内置功能。因此,要通过 win_command 运行 mklink,您必须运行 cmd.exe 程序并传递一个参数来告诉它执行“mklink”的作用,如下所示:

win_command: cmd.exe /k mklink log D:\prod\log


推荐阅读