Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: ignore errors rule #2413

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/ansiblelint/rules/ignore_errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# ignore-errors

This rule checks that playbooks do not use the `ignore_errors` directive to ignore all errors.
Ignoring all errors in a playbook hides actual failures, incorrectly mark tasks as failed, and result in unexpected side effects and behavior.

Instead of using the `ignore_errors: true` directive, you should do the following:

- Ignore errors only when using the `{{ ansible_check_mode }}` variable.
- Use `register` to register errors.
- Use `failed_when:` and specify acceptable error conditions.

## Problematic Code

```yaml
---
- name: Example playbook
hosts: all
tasks:
- name: Run apt-get update
ansible.builtin.command: apt-get update
ignore_errors: true # <- Ignores all errors, including important failures.
```

## Correct Code

```yaml
---
- name: Example playbook
hosts: all
tasks:
- name: Run apt-get update
ansible.builtin.command: apt-get update
ignore_errors: "{{ ansible_check_mode }}" # <- Ignores errors in check mode.
```

```yaml
---
- name: Example playbook
hosts: all
tasks:
- name: Run apt-get update
ansible.builtin.command: apt-get update
ignore_errors: true
register: ignore_errors_register # <- Stores errors and failures for evaluation.
```

```yaml
---
- name: Example playbook
hosts: all
tasks:
- name: Disable apport
become: "yes"
lineinfile:
line: "enabled=0"
dest: /etc/default/apport
mode: 0644
state: present
register: default_apport
failed_when: default_apport.rc !=0 and not default_apport.rc == 257 # <- Defines conditions that constitute a failure.
```