Bootstrap FreeKB - Ansible - Ignore errors using the ignore_errors paramater
Ansible - Ignore errors using the ignore_errors paramater

Updated:   |  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 (this article)
  • Use meta: clear_host_errors to clear errors so that subsequent tasks are run
  • 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

There are a number of different Ansible tasks that can cause a playbook to cease execution, typically when the return code of a task is anything other than 0. As an example, 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: localhost
  tasks:
  - lineinfile:
      path: /tmp/foo.txt
      line: Hello World

  - debug:
      msg: "I am here"
...

 

Invoking this playbook will return fatal, like this, and the playbook would cease to move onto the remaining tasks.

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Destination /tmp/foo.txt does not exist !", "rc": 257}

 

Here is how the ignore_errors parameter could be used.

---
- hosts: localhost
  tasks:
  - lineinfile:
      path: /tmp/foo.txt
      line: Hello World
  ignore_errors: true

  - debug:
      msg: "I am here"
...

 

Invoking the playbook should now return the following. Notice the lineinfile task contains ...ignoring and the debug task is processed and the play recap has ignore=1.

TASK [lineinfile] 
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Destination /tmp/foo.txt does not exist !", "rc": 257}
...ignoring

TASK [debug] 
ok: [localhost] => {
    "msg": "I am here"
}

PLAY RECAP 
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a9f297 in the box below so that we can be sure you are a human.