Video Transcript
So far, we have seen plays with tasks only, but they can also contain roles. A role is a way to group tasks. They are not targeted to specific hosts so that they can be reused in different plays. They can be invoked with the keyword roles:
.
To create a role, you have to make the folder roles
in your playbook’s path. Inside the roles
folder, some other folders are needed; at least the folder tasks
must be present. However, you can have different folders like: files
for any file required by the role, templates
for template files, vars
where you can declare specific variables for the role only, and defaults
for the default values for any variable set within the roles.
In this example, we have in the roles folder the role cobblerd
. The role is executed from the playbook in this way. And as you can see, the tasks in the role are not targeted to any host; they are just a list of tasks; we will install Cobbler and start some services. To run it, you type the command ansible-playbook -i inventory simple.yml
. Once it is finished, you will see no changes were made. Let’s stop named service to force one change, as seen in the output.
As we said before, roles are reusable because they are not targeted against any host. In this example, we will reuse the role cobblerd
against the hosts of the group clusters
, which are the master nodes you are using right now. We will execute this command in master8
because it is the only free cluster. To restrict the execution to master8, we use the -l
flag and press enter. We see Cobbler was installed and that the service that needed to be enabled and restarted was cobblerd
. In this part of the exercise, you will complete this role, cobblerd
, so that you can make your full Cobbler configuration using Ansible.