Ansible include task only if file exists


I’m trying to include a file only if it exists. This allows for custom “tasks/roles” between existing “tasks/roles” if needed by the user of my role. I found this:

But the Ansible docs state that:

“All the tasks get evaluated, but the conditional is applied to each and every task” –


Will fail if the file being included doesn’t exist. I guess there might be another mechanism for allowing a user to add tasks to an existing recipe. I can’t let the user to add a role after mine, because they wouldn’t have control of the order: their role will be executed after mine.


Below code seems to work, 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 and error with: “sudo: a password is required\n”

Leave a Reply