如何检查ansible窗口中是否存在文件?

Sha*_*uan 6 ansible

我在Windows上使用Ansible,必须检查C:\ Temp中是否存在文件。如果文件不存在,那么我必须跳过任务。我正在尝试使用win_stat模块,而这是我无法使用的模块:

- name: Check that the ABC.txt exists
  win_stat:
    path: 'C:\ABC.txt '      

- name: Create DEF.txt file if ABC.txt exists
  win_file:
    path: 'C:\DEF.txt'
    state: touch
  when: stat_file.stat.exists == True 
Run Code Online (Sandbox Code Playgroud)

Sha*_*uan 6

所以我没有正确使用win_stat模块,

应该在我的第一个“任务”中添加register参数。

这是这样的-

- name: Check that the ABC.txt exists
  win_stat: path= 'C:\ABC.txt'  
  register: stat_file

- name: Create DEF.txt file if ABC.txt exists 
  win_file:
    path: 'C:\DEF.txt'
    state: touch
  when: stat_file.stat.exists == True
Run Code Online (Sandbox Code Playgroud)


Ale*_*itz 5

当我尝试上面的答案时,它给了我一个语法错误,而我必须这样写:

- name: Check that the ABC.txt exists
  win_stat: 
    path: 'C:\ABC.txt'  
  register: stat_file

- name: Create DEF.txt file if ABC.txt exists 
  win_file:
    path: 'C:\DEF.txt'
    state: touch
  when: stat_file.stat.exists == True
Run Code Online (Sandbox Code Playgroud)