I've searched for examples on how to stop execution of an Ansible task without failing it.
A straightforward example:
- name: check file
stat: path={{some_path}}
register: check_file_result
- name: if file exists, stop
**fail**: msg="file exists, stopping"
when: check_file_result.stat.exists
This works as in it stops the execution, but fails with tons of red flavoured text probably stopping whole playbook from running further tasks. Is there a way to stop the execution as if all of it ended with "OK"?
Note: Workaround is to simply add "when: check_file_result.stat.exists == false", but this scales very poorly.
Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.
The default behavior is to pause with a prompt. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early: press ctrl+c and then c . To abort a playbook: press ctrl+c and then a .
Handlers and Failure If a task later on in the same play fails, the service will not be restarted despite the configuration change. You can change this behavior with the --force-handlers command-line option, or by including force_handlers: True in a play, or force_handlers = True in ansible.
warnings can be disabled by setting deprecation_warnings=False in ansible. cfg.
If there are multiple tasks you don't want to execute when check_file_result.stat.exists
then I would probably encapsulate all those tasks in a separate file and then conditionally include
that file, e.g.
- name: check file
stat: path={{ some_path }}
register: check_file_result
- include: file_ops.yml
when: check_file_result.stat.exists
Example directory structure:
|- roles/
|- foo
|- tasks
|- main.yml
|- file_ops.yml
|- vars
|- main.yml
Using this method is DRY :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With