3 Mins Read

Using Unparalleled Ansible Roles for Modular Configuration Management


In the world of configuration management, maintaining consistency and organization is key to efficient operations. Ansible, a powerful open-source automation platform, offers a concept known as “roles” to achieve just that. Roles in Ansible provide a structured way to organize configuration tasks, making them reusable across different projects and environments. This blog provides step-by-step instructions for using Ansible for Modular Configuration Management.

Using Ansible for Modular Configuration Management

  • Understanding Ansible Roles: An Ansible role is a self-contained unit that encapsulates tasks, handlers, variables, and other configuration elements needed to perform a specific function. Roles are designed to be reusable and shareable, allowing you to break down complex configurations into manageable components.
  • Organizing Configuration Tasks: Roles offer a structured approach to organizing configuration tasks. Instead of having a monolithic playbook with a long list of tasks, you can create roles for specific functions or components of your infrastructure. For example, you might have roles for setting up web servers, databases, and load balancers. This modular approach not only improves readability but also simplifies maintenance and troubleshooting.
  • Reusability Across Projects: One of the most powerful features of roles is their reusability. Once you’ve defined a role for a specific task, you can easily use it across different projects or environments. This means you can maintain a library of roles that cover common infrastructure components, saving you time and effort in future projects.
  • Role Dependencies: Roles in Ansible can have dependencies on other roles. This means that a role can rely on one or more other roles to provide certain functionality. For example, a role for setting up a web server might have a dependency on a role that configures the underlying operating system. This promotes modularity and allows you to build on existing roles.
  • Role Directory Structure: Ansible provides a standardized directory structure for organizing roles. This structure includes directories for tasks, handlers, defaults, variables, templates, and more. This convention helps maintain consistency across roles and makes it easy for other team members to understand and contribute to the codebase.

Example Use Case: LAMP Stack Role

As an example, let’s consider a common use case: setting up a LAMP (Linux, Apache, MySQL, PHP) stack. By creating a LAMP role, you can encapsulate all the tasks, variables, and templates required to install and configure these components. This role can then be reused in multiple projects, ensuring a consistent setup every time.

Next let us explore step by step instructions to using Ansible for Modular Configuration Management.

Customized Cloud Solutions to Drive your Business Success

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

Step 1: Create a Roles Directory

If you haven’t already, create a directory to house your roles. This directory should be named roles and located in the same directory as your main playbook.

mkdir roles

Step 2: Download the LAMP Stack Role

You can use an existing LAMP stack role from Ansible Galaxy. For this example, we’ll use the geerlingguy.apache role, which sets up Apache on your target servers.

ansible-galaxy install geerlingguy.apacheapache

Step 3: Integrate the Role in Your Playbook

In your main playbook file (e.g., main.yml), specify the roles you want to include. Add the geerlingguy.apache role along with any other roles you may need.

-name: Setup LAMP Stack
hosts: web_servers
become: yes
- geerlingguy.apache

Step 4: Define Role Variables (Optional)

If the geerlingguy.apache role requires any specific variables to be customized, you can define them in your playbook. For example, you might want to specify custom configurations for Apache.
- name: Setup LAMP Stack
hosts: web_servers
become: yes
- { role: geerlingguy.apache, apache_vhosts: [{servername: '', documentroot: '/var/www/html'}] }

Step 5: Execute Your Playbook

Run your playbook, and Ansible will automatically include and execute the tasks defined in the geerlingguy.apache role.

ansible-playbook main.yml

Step 6: Observe the Execution

Ansible will now execute the tasks defined in the geerlingguy.apache role for the specified hosts. This includes tasks for installing Apache, configuring virtual hosts, and managing other related tasks.

By using the geerlingguy.apache role, you’ve automated the setup of the Apache web server, saving you time and ensuring a consistent configuration across your web servers. This is just one example of how Ansible roles can simplify the automation of complex tasks.


In conclusion, Ansible roles are a powerful tool for modular configuration management.  Using Ansible for Modular Configuration Management allow you to organize and reuse configuration tasks across different projects, promoting consistency and efficiency in your automation workflows. By adopting roles, you can simplify your Ansible playbooks, making them more readable, maintainable, and adaptable to evolving infrastructure needs. Using roles in Ansible provides a structured and reusable way to manage configuration tasks, making your playbooks more organized and efficient. It also simplifies collaboration with team members and allows for easy integration of community-contributed roles.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, AWS EKS Service Delivery Partner, and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

You can learn more about our DevOps training offerings from our website.

WRITTEN BY Sruti Samatkar



    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!