0%

0/1 Lessons

Course Introduction

• 16min

0 / 3 lessons complete

IT Lab Setup

• 20min

0 / 3 lessons complete

Installing Ansible

• 35min

0 / 3 lessons complete

Managing your Ansible Inventory

• 2hr 7min

0 / 8 lessons complete

Ansible Basics

• 1hr 43min

0 / 9 lessons complete

Ansible Roles

• 1hr 46min

0 / 8 lessons complete

Ansible Galaxy

• 1hr 33min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 1hr 51min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 1hr 41min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Ansible Roles Overview

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.

Create note

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 NameConfigured Roles
          Web ServerCommon, Web Server
          Database ServerCommon, Database Role, Restricted Access
          Management ServerCommon
          Ansible Roles Modular Example

          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.

          0 0 votes
          Lesson Rating
          Subscribe
          Notify of
          profile avatar
          0 Comments
          Inline Feedbacks
          View all comments