commandansiblemklink

When using "win_command: mklink softlinkfile destfile" can not locate mklink.exe\"


I'm using ansible to manage several windows hosts on the cloud, I need to create a log file and link it to another file, so I use the 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

when running this playbook, the hosts can be found successfully, but I got the following error report

> 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}

and I tryed this command on the remote host in the same directory, it can be execute successfully. I don't know what to do......


Solution

  • win_command is for running executables directly. Hence the user's environment isn't applied and you aren't running inside a dos box or powershell window

    so 'mklink' isn't actually an executable - its a built-in feature of the cmd.exe program. So to run mklink via win_command, you'd have to run the cmd.exe program and pass it an argument to tell it do what 'mklink' does, like this:

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