How to Build Reusable and Scalable Automation with Ansible Roles
Ansible roles are a way to organize your Ansible automation tasks in a more structured and modular way. In essence, a role is a collection of tasks, templates, files, and other components that together define a set of related automation tasks. Roles are designed to be reusable and shareable, which makes them a powerful tool for automating infrastructure and configurations across multiple systems.
Roles can be included in Ansible playbooks using the “roles” keyword. When you include a role in a playbook, Ansible automatically executes the tasks defined in the role. This makes it easy to reuse roles across multiple playbooks and systems.
Overall, Ansible roles are a powerful feature that can help you streamline your automation tasks, make your code more modular and reusable, and promote best practices for infrastructure and configuration management.
Here are the steps to create and use Ansible Roles: 🛹✈
👉 Step 1: Create a New Role ✍
To create a new role, use the “ansible-galaxy” command and specify the name of the role you want to create. For example, to create a role called “webserver”, run the following command:
This will create a new directory called “testamey12” in your current directory. This directory contains the basic structure of an Ansible role, including subdirectories for tasks, handlers, templates, and files
If you want to see Role Directory Structure
👉 Step 2: Define the Tasks ✍
The tasks directory is where you define the actions that your role will perform. For example, to install Git, create a new file in the tasks directory called “install.yml” and add the following code:
This code uses the “apt” module to install the git.
👉 Step 3 : Create another new file “user.yml” in the task directory which will create new user ✍
This code will create new user when we run the playbook.
👉 Step 4 : We need to call tasks in main.yml which is under the role “testamey12” ✍
👉 Step 5: Include the Role in a Playbook ✍
Once you have defined your role, you can include it in a playbook using the “roles” keyword. For example, to apply the “testamey12” role to a set of servers, create a new playbook called “testrole.yml” and add the following code:
This code uses the “hosts” keyword to specify the target servers, and includes the “testamey12” role using the “roles” keyword.
👉 Step 6 : Run the playbook ✍
Woohooo ✨✌🐱🏍 you have successfully created user and install software on the target server.
Now you can check your target server that if git is installed or not and new user is created or not.
👉 To check 👀 use below commands:
- Check git version
2. Check user is created or not.
Conclusion:
Ansible roles provide a powerful way to organize and modularize your automation tasks. By breaking down your tasks into smaller
“Together we can achieve great things. Let’s collaborate and make a difference.” ✌🐱🚀
Follow me on 😎
For any queries : Linkedin : 🐱🏍🐱🚀
Code available on : Github : 🐱🏍🐱🚀
Thank you for reading 🤞 If you have any questions or feedback, feel free to leave a comment below. Keep practicing and happy learning! 🤝🙏