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:
- include: ...
when: condition
But the Ansible docs state that:
"All the tasks get evaluated, but the conditional is applied to each and every task" - http://docs.ansible.com/playbooks_conditionals.html#applying-when-to-roles-and-includes
So
- stat: path=/home/user/optional/file.yml
register: optional_file
- include: /home/user/optional/file.yml
when: optional_file.stat.exists
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.
The
with_first_found
conditional can accomplish this without astat
orlocal_action
. This conditional will go through a list of local files and execute the task withitem
set to the path of the first file that exists. Includingskip: true
on thewith_first_found
options will prevent it from failing if the file does not exist.Example:
If I am not wrong, you want to continue the playbook even the when statement false?
If so, please add this line after
when:
So your tasks will be look like this:
Please let me know, if I understand your question correctly, or can help further. Thanks
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 and error with: "sudo: a password is required\n"The best option I have come up with so far is this:
It's not exactly an "if-exists", but it gives users of your role the same result. Create a few variables within your role and a default empty file. The Jinja filters "default" and "lookup" take care of falling back on the empty file in case the variable was not set.
For convenience, a user could use the
{{ playbook_dir }}
variable for setting the paths:I using something similar but for the file module and what did the trick for me is to check for the variable definition, try something like:
the task will run only when the variable exists.
Using ansible-2.1.0, I'm able to use snippets like this in my playbook:
I get no errors/failures when
local.yml
does not exist, and the playbook is executed (as a playbook, meaning it starts with thehosts:
line, etc.)You can do the same at the task level instead with similar code. Using
stat
appears to work correctly.