Ansible Include Task Only If File Exists
Answer : 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 Thanks all for your help! I'm aswering my own question after finally trying all responses and my own question's code back in today's Ansible: ansible 2.0.1.0 My original code seems to work now, except the optional file I was looking was in my local machine, so I had to run stat through local_action and set become: no for that particular tasks, so ansible wouldn't attempt to do sudo in my local machine ...