Ansible - Clearing fatal errors using meta clear_host_errors
by
Jeremy Canfield |
Updated: June 21 2023
| Ansible articles
There are a number of ways to handle fatal errors.
- Use fail to end a play and return "failed"
- Use the --check command line flag to run the playbook in a dry run mode
- Use check_mode to determine if any fatal errors would be returned
- Use failed_when to continue based on the task return code
- Use ignore_errors to ignore errors returned by a task
- Use meta: clear_host_errors (this article)
- Use meta: end_play to end a play for all hosts
- Use meta: end_host to end a play for certain hosts and return "skipped"
- Use block rescue always to run tasks even after a fatal error is returned
- Use stat to determine if /tmp/foo.txt exists and the when parameter to skip the lineinfile module if /tmp/foo.txt does not exist.
- Use stat to determine if /tmp/foo.txt exists and then use the file module and the when parameter to create /tmp/foo.txt if the file does not exist
The meta module with clear_host_errors can be used to clear fatal and fail errors.
The ansible-doc meta command can be used to show the Ansible documention on the meta module.
Let's say /tmp/foo.txt does not exist and you attempt to append "Hello World" to /tmp/foo.txt using the lineinfile module.
---
- hosts: all
tasks:
- name: append 'Hello World' to /tmp/foo.txt
lineinfile:
path: /tmp/foo.txt
line: Hello World
...
When the target file does not exist, invoking this playbook will return a fatal error, like this.
TASK [append 'Hello World' to /tmp/foo.txt]
fatal: [server1.example.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Destination /tmp/foo.txt does not exist !", "rc": 257}
The meta module with clear_host_errors will change this behavior, so that server1.example.com is processed in the subsequent tasks.
---
- hosts: all
tasks:
- name: append 'Hello World' to /tmp/foo.txt
lineinfile:
path: /tmp/foo.txt
line: Hello World
- meta: clear_host_errors
...
Did you find this article helpful?
If so, consider buying me a coffee over at
Comments
September 29 2019 by Tony
Good afternoon. I am planning to buy one of these thinclients: https ://thinclientbenefits.com/products/wyse-5070 the "Extended" option. But need to know if I can install a Linux distro on it (for example TinyCorePlus by following your walkthru, or the procedure does not apply anymore to newer thinclient generations?
If that is the case, what can you suggest I should do? I plan to use the machine as an HTPC, so I NEED to get rid of the thinclient's OS and install a "household" :P OS, so any suggestion will be welcome.
Thank you for your attention.