2016-05-07 118 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

你可以简单地添加另一个条件,当它不存在时做任何你想做的事情,这样它就不会被跳过 - 更新了答案。 –