Bobcares

Try our smart search search

Stuck with Non-Zero return code: Ansible error? We can help you.

Ansible will fail if the exit status of a task is any non-zero value.

As part of our Server Management Services, we assist our customers with several Ansible queries.

Today, let us see how we can fix this error.

 

Non-Zero return code: Ansible

Generally, the error looks like this:

TASK [Non-Zero return] 
**********************************************************************************
fatal: [server1.lab.com]: FAILED! => {“changed”: true, “cmd”: “ls | grep wp-config.php”, “delta”: “0:00:00.021103”, “end”: “2021-06-29 12:53:49.222176”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2021-06-29 12:53:49.201073”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
fatal: [server2.lab.com]: FAILED! => {“changed”: true, “cmd”: “ls | grep wp-config.php”, “delta”: “0:00:00.021412”, “end”: “2021-06-29 12:53:50.697567”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2021-06-29 12:53:50.676155”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
fatal: [server3.lab.com]: FAILED! => {“changed”: true, “cmd”: “ls | grep wp-config.php”, “delta”: “0:00:00.015554”, “end”: “2021-06-29 12:53:50.075555”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2021-06-29 12:53:50.060001”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}

In common, if a command exits with a zero exit status it means it has run successfully.

On the other hand, any non-zero exit status of the command indicates an error.

For example,

$ date
Tuesday 29 June 2021 05:21:28 PM IST
$ echo $?
0Copy Code

Here, we can see the successful execution of the shell command “date”. Hence, the exit status of the command is 0.

A non-zero exit status indicates failure. For example,

$ date yesterday
date: invalid date ‘yesterday’
$ echo $?
1Copy Code

Here, the argument for the ‘date’ command, “yesterday”, is invalid. Hence, the exit status is 1, indicating the command ended in error.

However, though we execute properly, there are some commands which return a non-zero value.

$ ls | grep wp-config.php
$ echo $?
1Copy Code

Here, the wp-config.php file doesn’t exist in that directory. Even though the command executes without error, the exit status is 1.

By default, Ansible will report it as failed.

 

How to resolve the problem?

The best practice in order to solve this is to avoid the usage of shell command in the playbook.

Instead of the shell command, there is a high chance for an ansible module that does the same operation.

So, we can use the ansible built-in module find which allows locating files easily through ansible.

Alternatively, we can define the condition for a failure at the task level with the help of failed_when.

For example,


 hosts: all
tasks:
 name: Non-Zero return
shell: “ls | grep wp-config.php”
register: wp
failed_when: “wp.rc not in [ 0, 1 ]Copy Code
TASK [Non-Zero return]
***********************************************************************************************************
changed: [server1.lab.com]
changed: [server2.lab.com]
changed: [server3.lab.com]

Though the exit status is not Zero, the task continues to execute on the server.

Here, the exit status registers to a variable and then pass through the condition. If the return value doesn’t match the condition, only then the task will report as a failure.

On the other hand, we can ignore the errors altogether.

For that, we use ignore_errors in the task to ignore any failure during the task.


 hosts: all
tasks:
 name: Non-Zero return
shell: “ls | grep wp-config.php”
ignore_errors: trueCopy Code
ASK [Non-Zero return]
***********************************************************************************************************
fatal: [server1.lab.com]: FAILED! => {“changed”: true, “cmd”: “ls | grep wp-config.php”, “delta”: “0:00:00.004055”, “end”: “2021-06-29 13:09:20.631570”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2021-06-29 13:09:20.627515”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
…ignoring
fatal: [server2.lab.com]: FAILED! => {“changed”: true, “cmd”: “ls | grep wp-config.php”, “delta”: “0:00:00.006745”, “end”: “2021-06-29 13:09:22.110059”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2021-06-29 13:09:22.103314”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
…ignoring
fatal: [server3.lab.com]: FAILED! => {“changed”: true, “cmd”: “ls | grep wp-config.php”, “delta”: “0:00:00.004957”, “end”: “2021-06-29 13:09:21.465326”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2021-06-29 13:09:21.460369”, “stderr”: “”, “stderr_lines”: [], “stdout”: “”, “stdout_lines”: []}
…ignoring

By default, for ansible to recognize the task complition, the exit status must be Zero. Otherwise, it will fail.

We can manipulate the exit status of the task by registering the return value to a variable and then use conditional to determine if the task fails or succeeds.

To continue the playbook, in spite of the failure, we can use the ignore_errors option on the task.

[Confused with the procedure? We are here for you]

 

Conclusion

In short, we saw how our Support Techs fix the Ansible error for our customers.