File: ignore_errors.md

package info (click to toggle)
ansible-lint 25.6.1%2Breally25.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,628 kB
  • sloc: python: 18,973; sh: 66; makefile: 7
file content (61 lines) | stat: -rw-r--r-- 1,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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.
```