首页 > 解决方案 > 我需要使用 ansible 以下命令执行: FOR /f "skip=7 delims=" %%a IN (' DIR D:\backups\auto\ /od /b') DO RD /S /Q "D:\backups \自动\%%a"

问题描述

我需要使用 ansible 以下命令执行:

FOR /f "skip=7 delims=" %%a IN (' DIR D:\backups\auto\ /o-d /b') DO RD /S /Q "D:\backups\auto\%%a"

试过这个:

- name: clear old backups
  win_command: 
    FOR /f "skip=7 delims=" %%a IN (' DIR D:\backups\auto\ /o-d /b') DO RD /S /Q "D:\backups\auto\%%a"

标签: ansible

解决方案


虽然win_command:文档对这一事实保持沉默,但根据阅读win_shell:文档显示 PowerShell.exe 是默认值,而不是 CMD.exe

在文档中,它说您可以通过在任务中win_shell:指定来明确选择使用 CMD.exe 而不是 powershell :executable: cmd.exeargs:

- name: clear old backups
  win_shell: 
    FOR /f "skip=7 delims=" %%a IN (' DIR D:\backups\auto\ /o-d /b') DO RD /S /Q "D:\backups\auto\%%a"
  args:
    executable: cmd.exe

win_command:出于不允许 shell 运算符的相同原因,必须使用win_shell:才能访问 shell 内置函数,例如FORIF表达式


推荐阅读