0

I am trying to validate the file path if file exists or not. I have written below task.

 - 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

"fail" module is throwing below error

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

I am not getting why fail modules is by behaving as it suppose to.

4

1 回答 1

2
--- # 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.
于 2016-05-08T04:34:21.407 回答