Ansible

Ansible Block

Ansible is one of the most renowned free and open-source automation tools. However, even when automating tasks, things can go wrong. For example, a command may fail, a service may fail to start, a package may fail to start, etc. There are a variety of reasons why automated tasks may fail.

In this tutorial, we will learn how we can use the Ansible block to learn how to anticipate and handle the errors that may occur in an automated task.

What Is an Ansible Block?

In Ansible, blocks allow us to create logical groups of tasks. Blocks are an exceptional tool for handling the task errors like an exception block.

The following shows a typical syntax of an Ansible block:

block:
  - # Ansible Tasks.
rescue:
  - # If the tasks fail, run these tasks
always:
  - # Tasks that will always run, whether the block succeeds or fails...

The given illustration demonstrates the basic functionality of Ansible blocks.

Example 1: Grouping the Tasks

The following example playbook demonstrates how you can use Ansible blocks to group the tasks that should be skipped if a certain condition is met:

---
- name: Install and configure nginx
  tasks:
    block:
      - name: Install nginx
        apt:
          name: nginx
          state: present

      - name: Copy nginx configuration
        copy:
          src: files/nginx.conf
          dest: /etc/nginx/nginx.conf

    when: ansible_os_family == "Debian"

In this case, installing and copying the configuration file will only run if the operating system is Debian.

Example 2: Error Handling

Another common use of the Ansible block is error handling. In the following example playbook, we use a block to log any errors that may occur when updating the system packages:

---
- name: Update and handle errors
  hosts: database_servers
  tasks:
    block:
      - name: Update all packages
        apt:
          upgrade: yes
    rescue:
      - name: Copy error log
        copy:
          src: /var/log/apt/history.log
          dest: /tmp/apt-error.log
      - name: Notify
        mail:
          subject: "Package update failed!"
          body: "Check the error log at /tmp/apt-error.log."
          to: "[email protected]"

In this case, if the package update fails, Ansible creates a log of the events that are defined in the rescue block and email the admin.

Example 3: “Always” Block

Sometimes, you may wish for some tasks to run whether the previous tasks are successful. This is very useful in independent tasks that do not rely on the previous tasks.

In such a case, you can use the “always” block which defines the tasks that run no matter what happens in the previous blocks.

Consider the example playbook as demonstrated in the following:

---
- name: Database backup and cleanup
  hosts: db_servers
  tasks:
    block:
      - name: Backup database
        command: /usr/bin/backup-db
    rescue:
      - name: Notify backup failure
        mail:
          subject: "DB Backup failed!"
          body: "Database backup encountered an error."
          to: "[email protected]"
    always:
      - name: Cleanup old backups
        command: /usr/bin/cleanup-backups

In this case, if the backup task fails, we will notify the database admin. However, even if it succeeds or fails, we will still clean up the old backup files.

Conclusion

We explored how we can use the Ansible block construct to create a flexible and robust method to group the tasks, apply the conditions collectively, and gracefully handle the errors.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list