25 lines
525 B
Markdown
25 lines
525 B
Markdown
## Error Handling
|
|
|
|
Use try-except blocks to handle errors gracefully:
|
|
|
|
```yaml
|
|
- name: Handle errors with rescue block
|
|
shell: "invalid-command"
|
|
register: result
|
|
ignore_errors: yes
|
|
|
|
- name: Display error message
|
|
debug:
|
|
msg: "Error occurred: {{ result.msg }}"
|
|
when: result.failed
|
|
|
|
# Use rescue block for specific error handling:
|
|
# - name: Task that may fail
|
|
# shell: "some-command"
|
|
# register: result
|
|
#
|
|
# - name: Handle failure
|
|
# debug:
|
|
# msg: "Failed task: {{ result.msg }}"
|
|
# when: result.failed
|
|
``` |