Ansible

Ansible Ignore Error

While configuring the task on another host device, Ansible errors are a common occurrence. They represent unique and possibly significant system states. Despite this, there may be some errors that we want to avoid so that the tasks will also execute and show the output if they are successfully executed. In this article, we’ll talk about Ansible errors and how to disregard them. We specifically demonstrate a technique to supress and ignore the failures with Ansible.

Contrasting to error fixing, avoiding failures entails continuing with the tasks as much as the activity in the Ansible playbook is unaffected. The ansible tool issues a warning message when it cannot finish a task or playbook while debugging a task. There are several causes, but it’s up to us to identify them and find a solution. Unfortunately, not all errors can be fixed. You can choose to start ignoring the errors if you don’t want to or if you are unable to resolve the issue.

The majority of controlling the managers in Ansible use this strategy while connecting with the target hosts in real-world scenarios. Ansible defaults to ceasing activities on a target device and continuing activities on some other servers whenever it returns a non-zero returned result from a statement or an error from a package. Although, there are certain situations where you might want to act differently. A returned result that is not zero occasionally denotes progress. Oftentimes, you might want the processing to halt on one server so that it stops on all hosts.

Ways of Ignoring the Errors in Ansible

In Ansible, different ways are used to carry out the playbook tasks if it shows the task failure. The following are the different ways that will help execute the task even if Ansible is showing errors:

1. Utilizing the Ignore_Errors=True Command

Even though the task continues to fail, the playbook continues to execute if you specify the ignore_errors=true command at the bottom of the activity. Despite caring about the task completion or failures, it still carries out the subsequent activity. If somehow the activity is unsuccessful, it moves on to the next one. If the activity is successful, it completes a process after that.

2. Utilizing the Check Mode in Ansible

Utilize the Boolean special variables, the Ansible check mode, which is defined to True once Ansible is in the checking method to bypass a task or disregard the failures on a task whenever the checking method version of Ansible is used.

3. Utilizing the Failed=When Command in Ansible Playbook

In Ansible, we can also utilize the failed_when conditional to specify whatever “failure” is implied for each activity. Similar to all Ansible conditional statements, the lists of numerous failed_when criteria are combined with an implicit. So, the task only fails if all conditions are satisfied.

Prerequisites to Ignore the Errors in Ansible

The Ansible configuration tool must comply with the necessary criteria to include the concrete example:

  • An Ansible main server or we can say a controlling server is necessary so that we can configure the commands on the target device.
  • We need to have the local hosts so that we can contact each of them to use the different ways of ignoring the errors in the Ansible software. We use the local host in this instance as a target remote server for the duration of the activity.
  • We write the playbooks, run the Ansible ignore error commands, and use the ansible-controller device to track the outcomes on distant hosts.

To assist the learner to grasp the principle of utilizing the ignore error in an Ansible playbook, let’s implement the following example:

Example: Utilizing the Ignore_Errors=True Command

This is the simplest example that uses Ansible for implementation where we include several tasks in the playbook and execute the tasks using the ignore error command. To do this, we first write the following code in the Ansible terminal:

[root@master ansible]# nano ignore_errors.yml

 

After the creation and launching of the ignore_errors.yml playbook, we now begin to enter the commands in the playbook. First, we use the “hosts” option, passing the supplied hosts as “localhost”. We  enter the “false” value in the “gather facts” argument so that we won’t be able to obtain an additional information about the local host when we run the playbook.

After that, we begin listing each task that we wish to complete under the “tasks” option. In the first task, we display the document that is non-existent in the Ansible directory. First, we pass the title of the task which we want to implement. Then, we use the command option and store the non-existent text document and use “ls” so that we can execute the command in the first task. After the first task, we use the ignore_errors=true command so that if the task above the ignore statement has a failure, it ignores the task and moves to the next task and execute it.

We list another task which is used. If the first task fails, the Ansible tool must execute the next task. Then, we use the debug parameter to run the task in the playbook.

- hosts: localhost
  gather_facts: false
  tasks:
      - name: List a non-existent file
        command: ls non-existent.txt
        ignore_errors: true
     
      - name: continue task after failing
        debug:
          msg: "Continue task after failure"

 

Now, we list enough tasks to execute and check the ignore error command. Now, we terminate the playbook and go back to the main terminal. After that, we run the playbook. For that, we use the following statement:

[root@master ansible]# ansible-playbook ignore_errors.yml

 

After executing the aforementioned command, we get the following output. As you see, the first task that lists a non-existent file shows a failure. But the second task is executed successfully because we ignored the first task by utilizing the ignore_error=true in the playbook.

Conclusion

We learned what the ignore error means in Ansible. We discussed how it functions in the Ansible playbook. We also discussed the different ways of ignoring the error in Ansible while executing the tasks. We implemented an example so that every concept is clear for the user.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content