top of page

Ansible Error Handling

Dec 22, 2023
2 min read

Ansible Error Handling

Error handling in Ansible is crucial for managing and maintaining infrastructure. Ansible provides several mechanisms for error handling. Here are some key concepts and strategies:




1.failed_when


You can use the failed_when and changed_when statements to control the conditions under which a task is considered failed or changed.


In Ansible, the failed_when parameter allows you to specify a condition under which a task should be considered failed even if it completes successfully. This can be useful in scenarios where a task's success doesn't necessarily mean the desired state is achieved.


[root@siddhesh ~]# cat error_handle.yml
---
- name: Verify Directory
  hosts: all
  tasks:
    - name: Create a directory
      file:
        path: /tmp/sid/testdirectory
        state: directory
      register: directory_result
      failed_when: "'already exists' in directory_result.msg"
[root@siddhesh ~]#

Explanation:

The file module is used to create a directory at the specified path.

The register keyword captures the result of the task in the variable directory_result.

The failed_when parameter checks if the substring "already exists" is present in the msg attribute of the directory_result. If it is, the task is considered failed.

Output :

[root@siddhesh ~]# ansible-playbook -i inventory error_handle.yml
PLAY [Verify Directory] *******************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [jenkins.builddevops.com]
TASK [Create a directory] *****************************************************************************************************************
fatal: [jenkins.builddevops.com]: FAILED! => {"changed": false, "failed_when_result": "The conditional check ''already exists' in directory_result.msg' failed. The error was: error while evaluating conditional ('already exists' in directory_result.msg): 'dict object' has no attribute 'msg'. 'dict object' has no attribute 'msg'", "gid": 1000, "group": "centos", "mode": "0775", "owner": "centos", "path": "/tmp/sid/testdirectory", "size": 6, "state": "directory", "uid": 1000}
PLAY RECAP ********************************************************************************************************************************
jenkins.builddevops.com    : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
[root@siddhesh ~]#

2.ignore_errors


In Ansible, the ignore_errors parameter is used to specify whether to ignore errors that occur during the execution of a particular task. When ignore_errors is set to yes for a task, Ansible will continue executing subsequent tasks even if the current task fails.


[root@siddhesh ~]# cat create_file.yml
---
 - name: create test file
   hosts: jenkinshost
   tasks:
     - name: create file1
       command: touch /tmp/siddhesh/task1.txt
       ignore_errors: True
     - name: create file2
       command: touch /tmp/task3.txt
[root@siddhesh ~]#

In this playbook, Task 1: create file1

The first task is named "create file1."

It uses the command module to execute the touch command, creating an empty file at the path /tmp/siddhesh/task1.txt.

The ignore_errors: True parameter is set, meaning that if this task fails (e.g., if the file already exists), Ansible will continue executing subsequent tasks without raising an error.


Task 2: create file2

The second task is named "create file2."

It also uses the command module to execute the touch command, attempting to create an empty file at the path /tmp/task3.txt.


Output :

[root@siddhesh ~]# ansible-playbook -i inventory create_file.yml
PLAY [create test file] *******************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************
ok: [jenkins.builddevops.com]
TASK [create file1] ***********************************************************************************************************************
fatal: [jenkins.builddevops.com]: FAILED! => {"changed": true, "cmd": ["touch", "/tmp/siddhesh/task1.txt"], "delta": "0:00:00.005335", "end": "2023-12-21 17:18:46.888905", "msg": "non-zero return code", "rc": 1, "start": "2023-12-21 17:18:46.883570", "stderr": "touch: cannot touch ‘/tmp/siddhesh/task1.txt’: No such file or directory", "stderr_lines": ["touch: cannot touch ‘/tmp/siddhesh/task1.txt’: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [create file2] ***********************************************************************************************************************
changed: [jenkins.builddevops.com]
PLAY RECAP ********************************************************************************************************************************
jenkins.builddevops.com    : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1
[root@siddhesh ~]#

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page