Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop Ansible task successfully, not failing?

Tags:

break

ansible

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.

like image 914
hauron Avatar asked Aug 07 '14 14:08

hauron


People also ask

How do you ignore failure in Ansible?

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'.

How do you stop Ansible tasks?

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 .

What happens when one task is failed in playbook?

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.

How do I ignore warnings in Ansible?

warnings can be disabled by setting deprecation_warnings=False in ansible. cfg.


1 Answers

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 :-)

like image 193
jabclab Avatar answered Dec 10 '22 01:12

jabclab