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
0:02 that are closely related to Tasks.
0:04 Tasks, which we just went over
0:05 are an atomic unit of instructing Ansible
0:07 to take some action.
0:08 Roles are a way of taking those grouped Tasks
0:11 and any corresponding variables
0:13 and using an Anisble-defined, file naming convention
0:16 and directory structure convention.
0:18 So that those grouped Tasks can be reused
0:20 for more than one server
0:22 or as part of more than one playbook.
0:23 It's much easier to understand this with an example.
0:26 In the following directory structure
0:27 we have two Roles: Common and Web Server.
0:30 As well as two variable files: All and Web Server.
0:34 The first Role, common, has two files that specify Tasks.
0:38 main.yml and git.yml
0:40 that are stored under the tasks directory
0:42 of the common directory.
0:44 There are also other subdirectories
0:46 such as handlers and templates
0:47 that we'll take a look at in future videos.
0:49 The subdirectory you'll most frequently use
0:51 as a part of Roles, is the tasks subdirectory.
0:54 Ansible defines that the directories contained
0:56 within the roles directory are the names of your Roles.
1:00 And the directories, and the files within a Role directory
1:04 such as common or webserver
1:05 are accessible when you run the Ansible commands
1:08 that understand this directory and file grouping structure.
1:12 In a couple of videos when we run our Ansible commands
1:14 you'll see that Ansible will automatically pick up Common
1:18 and the Tasks within it.
1:19 The second Role, webserver
1:20 uses the files, with variables in them
1:23 stored under the group_vars directory.
1:25 Both all and webserver.
1:27 And it also has multiple YAML files
1:29 with Tasks stored within those YAML files.
1:32 Those tasks can reference
1:33 the variables stored under the group_vars directory.
1:36 So why would we want to use roles?
1:38 The most important part is that they make
1:40 what we're creating, reusable.
1:41 Rather than just having a one-off bash script
1:43 to handle the configuration of web server
1:46 we can reuse a web server configuration across many servers
1:49 and many playbooks.
1:51 And when we want to make some changes
1:52 whether large or small
1:54 Roles make it easy for us to re-run those Tasks
1:56 and iterate on our configuration.
1:58 Once we get into the meat of creating our playbooks
2:00 you'll understand how important Roles are.
2:02 And if you don't use Roles
2:04 you're really killing power of Ansible's
2:05 implicit runtime configuration.
2:07 Ansible defines a file and directory structure for Roles
2:10 to make things easier on the developer.
2:13 That structure will become second nature to you
2:15 after you create a few playbooks.