Ansible Roles Overview
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will learn about Ansible Roles, a feature for efficient and organized Ansible playbooks. By understanding roles, you'll be able to structure large playbooks and re-use code across different projects. This lesson will introduce the concept of roles, their structure, and how to implement them in your Ansible playbooks.
Understanding Ansible Roles
Roles in Ansible are a way of grouping related tasks and other resources together. They are essentially frameworks for fully independent or interdependent collections of variables, tasks, files, templates, and modules. The primary goal of roles is to facilitate playbook organization and reusability. This is particularly useful when managing complex playbooks, as roles can be used to break down the configuration into manageable sections.
A typical role directory structure might look like this:
There can be even more folders included in a role directory for more advanced usage of Ansible, like library, module_utils, lookup_plugins. These go beyond the scope of this course but you can learn more about these at the official Ansible documentation if you have a need for such advance configuration.
Here is a link to Ansibles official docs on Roles if you are curious. Again, probably not needed for 95% of Ansible's use cases but they do exist so I want you to be aware of them.
Directory Structure and Their Purposes
---
- name: Install Apache server
apt:
name: apache2
state: present
---
- name: restart apache
service:
name: apache2
state: restarted
---
webserver_package: apache2
---
http_port: 80
---
dependencies:
- { role: common, some_parameter: 3 }
Understanding Role Execution
When Ansible executes a role, it automatically loads files from these directories in a specific order. It begins with variables from defaults
, then overrides them with anything in vars
. Tasks are run next, handlers are notified if needed, files and templates are used as specified in tasks, and finally, any dependencies listed in meta
are processed.
Role Modularity
Let's understand how Ansible Roles can simplify your management of your servers. Imagine you have created four roles (without worrying about the code inside of each role):
Now imagine you have three servers:
Ask yourself this, what roles would you apply to each server in your ansible playbook? We can mix and match our roles on these servers as desired. This would prevent us from needing to type the same set of tasks for each server. For example, we could assign the roles to our servers like so:
Server Name | Configured Roles |
Web Server | Common , Web Server |
Database Server | Common , Database Role , Restricted Access |
Management Server | Common |
The common role is used across all three servers, but we still have the ability to add the other roles as needed for each server. As you can see, this versatility allows us to quickly customize and deploy our servers by mix and matching the roles that we create. Ansible Roles are very powerful!
Conclusion
This overview of Ansible Roles provided you a clear understanding of how Ansible structures roles and what each directory is used for. By properly leveraging roles, you can create highly reusable and organized Ansible playbooks, making your automation much more efficient and scalable.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.