Table of contents
1.
Introduction
2.
Blocks in Ansible 
3.
Grouping with Blocks 
4.
Error Handling in Blocks
5.
Frequently Asked Questions
5.1.
What is Ansible?
5.2.
What is the Ansible playbook?
5.3.
Which language is used in Ansible?
5.4.
What kind of tool is Ansible?
5.5.
What are handlers in Ansible?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Blocks in Ansible

Author Gunjan Batra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ansible is an open-source IT automation tool that helps you create, manage, deploy, and use many other IT processes. Ansible helps you with the easy installation of software. The IT professionals mostly use it for automation in the deployment, cloud, maintenance of the systems, updates, and on almost everything. 

Ansible Image

Ansible requires instructions to perform even the basic task. For anything to perform, Ansible requires a set of instructions that will be written in simple script form for anything to perform.

Blocks in Ansible 

By using blocks, logical grouping of tasks is possible. Blocks in play handle the same error handling in another programming language. The activities that can be performed using loops can be easily applied by grouping in blocks.

Blocks in Ansible

Blocks offer two features as described below:

  • Grouping with Blocks
  • Error handling with Blocks

Grouping with Blocks 

 Most of the operations (except loops) that are performed on a single task, can be clubbed at the block level, making it considerably simpler to set data or instructions shared by all functions. The directive only applies to the tasks encircled by a block; it does not impact the block itself. It increases the code readability and reduces the lines of code.

name: display blocks
hosts:localhost
become: no
gather_facts:  no


tasks:
      -name: Working of Block


block:
  
   -name: message 1
        debug: 
         msg: “message 1 run{{my_value}}”
  -name: message 2
       debug: 
       msg: “message 2 run{{my_value}}”


 -name: message 3
     debug:
     msg:  “message 3 run{{my_value}}”
vars:
my_value: coding ninjas
  when: yes 
     become: true
     become_user: x
     ignore_errors: yes


handlers
-name: x
Debug:
msg: “I am the handler handling the blocks”

 

“names ”inside the block have been available since  Ansible 2.3. It is always recommended to use names inside the block for better readability of the code.

In the code shown below, it will run perfectly after checking the “when” conditon. Once the condition gets true it will run all the three tasks that are mentioned in the code. The ignore_errors also continues to run the playbook even if any tasks fail to perform.

Mostly block are used for error handling as in other programming languages.

Error Handling in Blocks

Blocks have a special ability to handle exceptions and errors similar to exception handling in any other programming language. 

You can control the error in a block using “rescue” and “always”.

Block image

Rescue runs the code when the block code fails to get delivered. It helps to change the status of the code. Even if the block code gets failed, it shows the status program run successfully by running the rescue code.

name: display blocks
hosts:localhost
become: no
gather_facts:  no


tasks:
      -name: Working of Block


block:
  
   -name: message 1
        debug: 
         msg: “message 1 run ninjas”
        failed_when:  yes
rescue:
  
    -name:   message 2
   debug:
     msg:  “message 2 run ninjas”

 

Rescue tasks get executed when the previous tasks get failed. This helps in changing the status of the code.

There also comes an always block which runs every time, it is similar to the finally block in java.

It doesn’t matter what is the status of the code, the “always” block will run every time.

name: display blocks
hosts:localhost
become: no
gather_facts:  no


tasks:
      -name: Working of Block


block:
  
   -name: message 1
        debug: 
         msg: “message 1 run ninjas”
        failed_when:  yes
rescue:
  
    -name:   message 2
   debug:
     msg:  “message 2 run ninjas”


always:
 -name:   message 3
   debug:
     msg:  “message 3 run ninjas”

 

The tasks in the block code get executed. In case they fail the rescue task run. It is optional to run rescue code, but always code gets executed every time. Play continues to run if the rescue code gets executed as the status code has been changed. Although it won’t show any fatal_erros or max_fail_percentage it will still record in the playbook statistics. 

Frequently Asked Questions

What is Ansible?

Ansible is an open-source IT automation tool that is mostly used by It professionals to automate their development, manage their work, and increase their productivity.

What is the Ansible playbook?

Ansible playbooks are lists of tasks that get executed automatically against the hosts. It has complete information on when, where, and what user command it should run.

Which language is used in Ansible?

Ansible uses Python, Powershell, and R Programming Language.

What kind of tool is Ansible?

Ansible is an open-source It automation tool, that requires very less coding knowledge and can be used to perform very simple tasks. It can be used to check the configuration, installation, or whether the service is running on the operating system.

What are handlers in Ansible?

Handlers are the tasks that are defined in the handler section and gest executed once it gets the notification from the notify. Handlers are used when your task dependency relies on a specific task's success or failure.

Conclusion

In this blog, you have learned about the Ansible blocks. We started with the introduction of Ansible. We have seen what are blocks and where it is used. We have seen how the grouping of tasks can be done using blocks. We have further discussed how blocks handle the exception and errors in Ansible.

To get more information about Ansible please refer to our blog Ansible Pip.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Live masterclass