Ansible Display使用" stat"自定义错误消息

时间:2016-05-07 13:03:03

标签: ansible-playbook ansible-2.x

我正在尝试验证文件路径如果文件存在与否。我写了下面的任务。

 - 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

我不知道为什么失败的模块是按照它的行为表现的。

1 个答案:

答案 0 :(得分:1)

--- # 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.