简体   繁体   English

Ansible - win_file 模块 - 如果文件正在使用,则强制删除

[英]Ansible - win_file module - force deletion if file in use

We are using ansible win_file module to delete a particular folder in a Windows Server machine with the following code:我们正在使用 ansible win_file 模块删除 Windows 服务器机器中的特定文件夹,代码如下:

- name: Delete <folderName> directory
  win_file: path=C:\<pathToFolder>\{{target_environment}}\<folderName> state=absent
  tags: <folderName>

The problem: When a file from that directory is open in another program at the same time the ansible role runs, it fails saying:问题:当 ansible 角色运行时,当该目录中的文件同时在另一个程序中打开时,它无法显示:

  • "The process cannot access the file because it is being used by another process" “该进程无法访问该文件,因为它正被另一个进程使用”

Now, i understand the error, but i am looking for suggestions to force this deletion, even if the file is in use, or if there is other module that i don't know that can't resolve this problem.现在,我理解了这个错误,但我正在寻找强制删除的建议,即使文件正在使用中,或者如果有其他我不知道的模块无法解决这个问题。

(currently using ansible 2.4.6) (目前使用ansible 2.4.6)

So, after some searching and digging, i came out with a solution, i found a similar ansible module that can do the job, win_shell .所以,经过一番搜索和挖掘,我想出了一个解决方案,我找到了一个类似的 ansible 模块,可以完成这项工作, win_shell

I resolved the problem with the following code:我用以下代码解决了这个问题:

  name: Delete <folderName> directory
  win_shell: Remove-Item –path <folderName> –recurse -force
  args:
    chdir: C:\<pathToFolder>\{{target_environment}}
    removes: C:\<pathToFolder>\{{target_environment}}\<folderName>
  tags: <folderName>
  • removes: checks if the folder exists else skips the task删除:检查文件夹是否存在,否则跳过任务
  • force: does the trick of what i want, delete the folder and all his files even if some of the files are in use or open in any program.强制:做我想要的伎俩,删除文件夹和他的所有文件,即使某些文件正在使用或在任何程序中打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM