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

时间:2017-04-06 07:32:39

标签: 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 

2 个答案:

答案 0 :(得分:5)

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

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

这就是它的工作原理 -

- 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

答案 1 :(得分:1)

当我尝试上面的答案时,它给了我一个语法错误,相反,我不得不这样写:

- 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
相关问题