From the course: Ansible Essential Training

An overview of roles in Ansible - Ansible Tutorial

From the course: Ansible Essential Training

An overview of roles in Ansible

- Roles provide us with a method for organizing and sharing our complex Ansible orchestrations. Roles let us automatically load related variable files, tasks, handlers, and other Ansible components based on a known file structure. This file structure looks like as follows. Notice, we have a roles directory. And then inside of that roles directly, there are sub-directories for things like our handlers, our tasks, any files that are needed, templates, variables, defaults. So it's just a really convenient structure that's going to contain all of the components of our Ansible solution. Now, Ansible Galaxy is a website dedicated to hosting and sharing these roles that you and I have created. Ansible Galaxy also provides a client to help us in creating roles. That client is actually part of the default Ansible installation. So I'll cover the Ansible Galaxy website with you a bit later on. But for right now, let's just see that Ansible client that is included by default in our control node. So one of the things that I did, if we take a look at... Let's go to the /etc/ansible/ directory. And if I do a listing, notice that I have made a roles directory inside of /etc/ansible/. And I did this because this is the default location that Ansible will be looking for roles. So it's nice to take advantage of that location, but notice you can store your roles anywhere you like. You can give the location of the role in the playbook if you need to. And you could also edit the Ansible config file, of course, to look for roles in additional location. So if I want to create that role structure for my Ansible solution, it couldn't be any easier. Thanks to the built in Ansible Galaxy client. So I just say ansible-galaxy. And by the way, notice I am in... This is very important. I am in the directory where I want the role to be created. And I'm glad I checked this because I actually need to move into that directory, don't I? All right. Now, we are in the roles directory. Notice, it is empty. And now, that I'm in the location where I want to create my role, I just say ansible-galaxy init. And then, I give my role name. So let me go ahead and call this just testrole1. And it says, nope, sorry. Permission denied. So what's happening here? Well, I'm in a location, /etc/ansible/, and in this role's location that is protected with permission settings. So I'm going to go ahead and just pseudo this, and we should create that role successfully. So now if we take a look inside of our roles directory, there is our testrole1. And if we move into that directory, we'll see that the Ansible Galaxy client has built the role structure for us. So, great. And if we go into the tasks sub-directory, for instance, and we take a look at what's in there, notice there is this main.yml. So it is not only creating the directories, but it's giving us these simple. little YAML files. And then, reminding us of what they're used for. In this case, we have the main.yml in the task directory. And this is for the tasks file for this testrole1. Pretty cool that the Ansible Galaxy client is built right in, and we're ready to create these file structures and these files that makes our roles a reality. We really want to leverage the power of these things where appropriate in Ansible. Because they make for reusability of our code and help us really manage our infrastructure, and make the most out of Ansible.

Contents