0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Playbook Error Handling

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, we will focus on managing errors within an Ansible playbook, particularly when an error is expected or planned as part of the playbook's logic. We'll use the scenario of the conditional_test.yml playbook, where the absence of a file is an expected outcome based on a conditional statement.

You will learn how to handle errors gracefully in Ansible playbooks, ensuring that your automation continues smoothly even when certain tasks fail.

Understanding Error Handling in Ansible

Error handling in Ansible is important when executing tasks that might fail under expected conditions. For example, in our conditional_test.yml playbook, the task to check for a file's existence might fail if the file was deliberately removed. Instead of letting this stop our playbook, we can configure our playbook to ignore / skip the error.

Modifying the Playbook for Error Handling

We will update the conditional_test.yml playbook to include a task that checks for the file's existence using the ls command. To handle potential errors, we'll use the ignore_errors directive, which tells Ansible to continue executing the playbook even if the task fails.

Open conditional_test.yml and add the following task at the end:

    - name: Check if the file exists
      command: ls ~/conditional_test_file.txt
      ignore_errors: True
      register: file_check

In this task, we're using the command module to run ls ~/conditional_test_file.txt. The ignore_errors: yes line ensures that even if the command fails (which it will if the file doesn't exist), the playbook will not stop executing. The output of the command is registered in the file_check variable.

Adding a Conditional Success Message

Next, let's add a task to display a success message if the file was correctly removed (i.e., if the previous task failed). We can use the file_check variable to determine if the command was successful or not.

Add this task below the file check task:

    - name: Confirm file removal
      debug:
        msg: "File was successfully removed"
      when: file_check.failed

This task uses the debug module to print a message confirming the file's removal. The when: file_check.failed condition checks if the previous task failed (which indicates that the file was successfully removed).

Testing the Updated Playbook

Run the playbook normally which will not delete the file

ansible-playbook conditional_test.yml

You should see output like the following:

Ansible Playbook Run
Ansible Playbook Run

Run the updated playbook with the command to delete the file:

ansible-playbook conditional_test.yml -e '{create_file: False}'

You should see that the playbook is completed. It should show some errors, but it will state ...ignoring after the error, and you should see a debug message stating the file was removed:

Ansible Playbook Error Handling
Ansible Playbook Error Handling

Conclusion

In this lesson, you have learned how to handle errors in Ansible playbooks. By using ignore_errors and conditional statements, you can ensure your playbooks are robust and capable of handling scenarios where certain failures are expected.

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments