Skip to main content
In this lesson, you will learn how to use blocks in Ansible to streamline task configuration, group related tasks, and improve error handling. In our example, we set up an all-in-one server with a MySQL database and an Nginx web server. The MySQL-related tasks run as the “db-user” and the Nginx-related tasks run as the “web-user”.

Initial Playbook Example

Below is a simple playbook that installs and starts both MySQL and Nginx services:

Adding User Escalation and Conditional Execution

To tailor this playbook for environments running CentOS, and to enforce different user privileges, we update the tasks by including become_user and when conditions. In this version, MySQL tasks run as “db-user” and Nginx tasks run as “web-user”:
Using the when condition and become_user on each task can quickly become repetitive. Blocks allow you to group tasks and assign shared directives once per group.

Grouping Tasks with Blocks

Blocks in Ansible help you logically group related tasks. This approach not only reduces redundancy but also improves error handling via rescue and always sections. Consider the following example:

Key Benefits of Using Blocks

When you group tasks within a block, you can define error recovery (rescue) and actions that run regardless of success (always). This ensures a comprehensive overview of your deployment status.

Conclusion

Blocks are a powerful feature in Ansible that make your playbooks more manageable and resilient. By grouping tasks and employing rescue mechanisms, you can reduce repetition and handle errors effectively. Practice with these concepts by creating your own playbook blocks to solidify your learning. For further guidance on Ansible best practices, be sure to check out the Ansible Documentation. Happy automating!

Watch Video

Practice Lab