[Ansible] – How to work with errors? How to ignore and or create conditionals to fail?

Ignore errors

To ignore errors use the attribute ignore_errors as the following example.

- name: be sure ntp is installed
  yum:
    name: ntp
    state: installed
  ignore_errors: yes

Force continue

For don’t stop the execution when an error is faced the attribute force_handlers can be used. Following an example.

---
hosts: all
force_handlers: yes
tasks:
 - name: web server is enabled
   service:
     name: httpd
     enabled: true

Conditional to fail

By the attribute, failed_when is possible to force the playbook stop/fail when some conditional occurs. Following an example.

tasks:
  - shell: /usr/local/bin/myshell.sh
    register: shell_result
    failed_when: "'Not found' in shell_result.stdout"

For further information see the Error Handling In Playbooks documentation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s