Introduction to Ansible Transcripts
Chapter: Ansible Core Concepts
Lecture: Roles

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Ansible Roles are another core concept that are closely related to Tasks. Tasks, which we just went over are an atomic unit of instructing Ansible
0:08 to take some action. Roles are a way of taking those grouped Tasks and any corresponding variables and using an Anisble-defined, file naming convention
0:17 and directory structure convention. So that those grouped Tasks can be reused for more than one server or as part of more than one playbook.
0:24 It's much easier to understand this with an example. In the following directory structure we have two Roles: Common and Web Server.
0:31 As well as two variable files: All and Web Server. The first Role, common, has two files that specify Tasks. main.yml and git.yml
0:41 that are stored under the tasks directory of the common directory. There are also other subdirectories such as handlers and templates
0:48 that we'll take a look at in future videos. The subdirectory you'll most frequently use as a part of Roles, is the tasks subdirectory.
0:55 Ansible defines that the directories contained within the roles directory are the names of your Roles.
1:01 And the directories, and the files within a Role directory such as common or webserver are accessible when you run the Ansible commands
1:09 that understand this directory and file grouping structure. In a couple of videos when we run our Ansible commands
1:15 you'll see that Ansible will automatically pick up Common and the Tasks within it. The second Role, webserver uses the files, with variables in them
1:24 stored under the group_vars directory. Both all and webserver. And it also has multiple YAML files with Tasks stored within those YAML files.
1:33 Those tasks can reference the variables stored under the group_vars directory. So why would we want to use roles?
1:39 The most important part is that they make what we're creating, reusable. Rather than just having a one-off bash script
1:44 to handle the configuration of web server we can reuse a web server configuration across many servers and many playbooks.
1:52 And when we want to make some changes whether large or small Roles make it easy for us to re-run those Tasks and iterate on our configuration.
1:59 Once we get into the meat of creating our playbooks you'll understand how important Roles are. And if you don't use Roles
2:05 you're really killing power of Ansible's implicit runtime configuration. Ansible defines a file and directory structure for Roles
2:11 to make things easier on the developer. That structure will become second nature to you after you create a few playbooks.

Talk Python's Mastodon Michael Kennedy's Mastodon