Check one of the file exist or not in ansible

Viewed 850

I am trying to check one of the file exist or not in ansible.

Code for checking single file, which works perfectly.

- hosts: all
  tasks:
  - name: Ansible check file exists example.
    stat:
      path: /Users/data/info.text
    register: file_details

  - debug:
      msg: "The file exists"
    when: file_details.stat.exists

But I want to check if /Users/data/info.text or /Users/data/info.html exist or not, here I'm trying to use or operator, but it is saying invalid yml file.

- hosts: all
  tasks:
  - name: Ansible check file exists example.
    stat:
      path: /Users/data/info.text or /Users/data/info.html
    register: file_details

  - debug:
      msg: "The file exists"
    when: file_details.stat.exists
1 Answers

Try this

    - stat:
        path: "{{ item }}"
      register: file_details
      loop:
        - info.text
        - info.html

    - debug:
        msg: The files exist
      when: file_details.results|map(attribute='stat.exists') is all
Related