The with_first_found
conditional can accomplish this without a stat
or local_action
. This conditional will go through a list of local files and execute the task with item
set to the path of the first file that exists.
Including skip: true
on the with_first_found
options will prevent it from failing if the file does not exist.
Example:
- hosts: localhost
connection: local
gather_facts: false
tasks:
- include: "{{ item }}"
with_first_found:
- files:
- /home/user/optional/file.yml
skip: true