2016-05-07 115 views
0

我想驗證文件路徑如果文件存在與否。我寫下了下面的任務。Ansible Display自定義錯誤消息「stat」

- name: File Validation 
    stat: path={{src_file_path}}{{filename}} get_md5=yes 
    register: file 
- fail: msg="Whoops! File does not exist.!" 
    when: file.stat.exists == False 

「失敗」模塊拋出下面的錯誤

TASK: [deploy-stack | fail msg="Whoops! File does not exist.!"] *************** 
failed: [192.168.36.128] => {"failed": true} 
msg: Whoops! File does not exist.! 

FATAL: all hosts have already failed -- aborting 

我沒有得到爲什麼失敗模塊是表現爲它想。

回答

0
--- # Using stat - Check if a file exist on the remote system 

- hosts: ec2 
    remote_user: ec2-user 
    become_method: sudo 
    gather_facts: no 
    connection: ssh 

    tasks: 

    - name: check if the file is present or not 
     stat: path=/opt/hello.yml 
     register: p 

    - debug: msg="Path exists and is a file" 
     when: p.stat.isreg is defined and p.stat.isreg 

    - debug: msg="do something here as the file is not present" 
     when: p.stat.isreg == False 
... 

# Prints msg when it exists or skips it. 
+0

謝謝Chandan,我想驗證本地主機上的文件。如果文件存在,我會將它複製到遠程服務器並驗證校驗和。 在這種情況下,如果文件路徑改變,我無法處理異常。 ansible正在「跳過」驗證和文件複製任務。 – tgcloud

+0

你可以簡單地添加另一個條件,當它不存在時做任何你想做的事情,這樣它就不會被跳過 - 更新了答案。 –