1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. Ansible Essentials: Simplicity in Automation Technical Overview

Introduction to Roles

Developed with
Red Hat
Start course
Duration1h 27m


Sprawling infrastructure and snowflake woes? Then Ansible is the solution you need!

Ansible is designed to be minimal in nature, consistent, secure and highly reliable! Ansible is a highly sought after skill in the marketplace with an extremely low learning curve for administrators, developers and IT managers.

The "Ansible Essentials: Simplicity in Automation Technical Overview" course introduces you to Ansible automation and configuration management, provisioning, deploying, and managing compute infrastructure across cloud, virtual, and physical environments.

By taking this course you'll learn just how easy it is to use Ansible to build consistent and repeatable infrastructure environments using Ansible playbooks.


In this video, we are going to be talking about the Ansible concept of roles. So roles in Ansible are essentially a predefined directory structure that we're utilizing to put individual pieces of our playbook into, to make it easier to reuse and share among groups. So using our introductory playbook as an example, I had kind of multiple different components in there. I had a task to install EPEL, I had a task to install python-binding for SELinux, and then I had some Apache installation and starting tasks. So kind of all of those together they don't really necessarily fit into one playbook. And that's where the concept of roles would come into play.

Basically I could take each individual component of that playbook and turn it into a role so that I would only have to call those roles if I need to run those specific tasks. So let's take a look at the directory structure to give you more of an idea of what I am talking about.

So this is the project with an embedded role. I have my site.yml file at the top, the same level as the roles directory. And inside the roles directory I have two individual roles. I have the common role and then I have a webservers role. Now a common role is typically used to describe a role that would be run against all of your machines. So if you have base management that needs to happen to all of your machines regardless of OS type or version, then those would go inside of a common role. And then similarly I have a webservers role, which again would go towards being run against a webserver host specifically so things like Apache install tasks and starting would happen inside of a webservers role.

You can see the directory structure is as follows. I basically have files, templates, tasks, handlers, etc., and these are where I would put the individual pieces of my playbook. So using the Ansible key components for playbooks, these are kind of self-explanatory in the sense that, the things that we did in our main playbook make sense of where you would put them inside of the role. So for example, I had several tasks inside of my basic playbook. Those tasks would go inside the tasks directory inside of this role. I also had var specified in the sites that I used previously, where I specified the httpd_port and max_clients. Those can be defined in the vars directory. I also showed a handlers example. Those would be defined in the handlers directory. And so on and so forth. It just really depends on what roles you want to create and how you can kind of chunk out your playbook from there.

So how would I actually call this playbook or call a role from a playbook? And basically it's really simple. This is a playbook with a roles example that just has a hosts declaration and a roles declaration in it. I am targeting my web hosts and I am using the roles keyword to target my roles. And so I have got my common role and my webservers role. And then it would run in order, again, just like a normal playbook, from top to bottom. So the common role tasks would happen first and then the webserver task would happen second.

Now if you are interested in exploring the concept of Ansible roles further, I would recommend checking out Ansible Galaxy which is our community site for sharing roles. It's a great site. You can browse by most popular roles and, you know, utilize them in your own Ansible environment. So that concludes our introduction on Ansible roles.

Join us in our next video where we are actually going to create the Ansible role structure with the ansible-galaxy command.


About the Author
Learning paths23

Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.

Jeremy holds professional certifications for both the AWS and GCP cloud platforms.